This app shows a home screen with a blue button. When you tap it, you go to a new screen with a welcome message. The navigation bar shows the title "Home" on the first screen.
iOS Swift
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: Text("Welcome to the second screen!")) {
Text("Go to Second Screen")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}
}
.navigationTitle("Home")
}
}
}
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
OutputSuccess
Important Notes
Always wrap NavigationLink inside a NavigationView to enable navigation features.
You can customize the tappable content with any view, not just text.
NavigationLink automatically adds the arrow icon on the right side to show it is tappable.
Summary
NavigationLink creates tappable items that open new screens.
Use NavigationView to enable navigation in your app.
You can use text, images, or any view as the link content.
Practice
(1/5)
1. What is the main purpose of NavigationLink in SwiftUI?
easy
A. To display images in a grid layout
B. To create a tappable item that opens a new screen
C. To fetch data from the internet
D. To style text with colors and fonts
Solution
Step 1: Understand NavigationLink role
NavigationLink is used to create tappable items that navigate to another view.
Step 2: Compare other options
Options A, B, and C describe unrelated tasks like styling, fetching data, or layout.
Final Answer:
To create a tappable item that opens a new screen -> Option B
Confusing NavigationLink with styling or data fetching
Thinking NavigationLink displays images only
Assuming NavigationLink is for layout purposes
2. Which of the following is the correct syntax to create a NavigationLink with a text label "Next" that opens DetailView()?
easy
A. NavigationLink(destination: Text("Next")) { DetailView() }
B. NavigationLink(destination: Text("Next"), DetailView())
C. NavigationLink { DetailView() } label: { Text("Next") }
D. NavigationLink(destination: DetailView()) { Text("Next") }
Solution
Step 1: Recall NavigationLink syntax
The correct syntax uses NavigationLink(destination: View) { LabelView }.
Step 2: Check each option
NavigationLink(destination: DetailView()) { Text("Next") } matches the correct syntax. NavigationLink(destination: Text("Next"), DetailView()) swaps parameters incorrectly. NavigationLink { DetailView() } label: { Text("Next") } uses a newer syntax and is valid in SwiftUI 3+, so it is also correct. NavigationLink(destination: Text("Next")) { DetailView() } swaps destination and label views wrongly.
Final Answer:
NavigationLink(destination: DetailView()) { Text("Next") } -> Option D
Quick Check:
Correct parameter order = NavigationLink(destination: DetailView()) { Text("Next") } [OK]
Hint: Destination first, label inside closure for NavigationLink [OK]
Common Mistakes:
Swapping destination and label parameters
Using incorrect closure syntax
Passing views in wrong order
3. What will happen when the user taps the text "Go to Profile" in this SwiftUI code?
NavigationView {
NavigationLink(destination: ProfileView()) {
Text("Go to Profile")
}
}
medium
A. The app navigates to ProfileView screen
B. Nothing happens because NavigationView is missing
C. The text changes color but no navigation occurs
D. The app crashes due to missing NavigationLink label
Solution
Step 1: Identify NavigationView and NavigationLink usage
The code wraps NavigationLink inside NavigationView, enabling navigation.
Step 2: Understand tap behavior
Tapping the text triggers navigation to ProfileView as destination is set.
Final Answer:
The app navigates to ProfileView screen -> Option A
Quick Check:
NavigationView + NavigationLink = navigation works [OK]
NavigationLink requires a closure with braces {} for the label view.
Step 2: Analyze given code
The label Text("Tap here") is outside NavigationLink without braces, causing syntax error.
Final Answer:
Missing braces {} around the label view -> Option A
Quick Check:
Label must be inside braces {} [OK]
Hint: Label view must be inside braces {} after NavigationLink [OK]
Common Mistakes:
Omitting braces for label view
Thinking destination can't be Text
Assuming NavigationView is mandatory for syntax
5. You want to create a list of items where each item navigates to a detail screen showing that item's name. Which code correctly uses NavigationLink inside a List to achieve this?
hard
A. ForEach(items) { NavigationLink(destination: DetailView(name: $0)) { Text($0) } }
B. List { NavigationLink(Text(item), destination: DetailView(name: item)) }
C. List(items, id: \.self) { item in NavigationLink(destination: DetailView(name: item)) { Text(item) } }
D. List(items) { item in NavigationLink { Text(item) } destination: { DetailView(name: item) } }
Solution
Step 1: Understand List with NavigationLink
Use List with identifiable items and NavigationLink inside closure for each item.
Step 2: Check each option's correctness
List(items, id: \.self) { item in NavigationLink(destination: DetailView(name: item)) { Text(item) } } correctly uses List with id and NavigationLink with destination and label. List { NavigationLink(Text(item), destination: DetailView(name: item)) } has wrong parameter order. ForEach(items) { NavigationLink(destination: DetailView(name: $0)) { Text($0) } } uses ForEach but misses id and may cause errors. List(items) { item in NavigationLink { Text(item) } destination: { DetailView(name: item) } } uses invalid syntax for NavigationLink.
Final Answer:
List(items, id: \.self) { item in NavigationLink(destination: DetailView(name: item)) { Text(item) } } -> Option C
Quick Check:
List + NavigationLink with destination and label = List(items, id: \.self) { item in NavigationLink(destination: DetailView(name: item)) { Text(item) } } [OK]
Hint: Use List with id and NavigationLink inside closure [OK]
Common Mistakes:
Wrong parameter order in NavigationLink
Missing id in List or ForEach
Using invalid NavigationLink syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.