0
0
iOS Swiftmobile~20 mins

NavigationStack in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NavigationStack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
navigation
intermediate
2:00remaining
What is the output when navigating with NavigationStack?
Consider this SwiftUI code snippet using NavigationStack. What will the user see after tapping the button labeled "Next"?
iOS Swift
import SwiftUI

struct ContentView: View {
  var body: some View {
    NavigationStack {
      VStack {
        Text("Home Screen")
        NavigationLink("Next", destination: Text("Detail Screen"))
      }
    }
  }
}
AThe app shows "Home Screen" but tapping "Next" does nothing.
BThe app crashes because NavigationStack requires a NavigationView wrapper.
CThe app shows only "Detail Screen" immediately without any navigation.
DThe app shows "Home Screen" and a button labeled "Next". Tapping "Next" navigates to a screen showing "Detail Screen".
Attempts:
2 left
💡 Hint
Think about how NavigationStack and NavigationLink work together to navigate between views.
ui_behavior
intermediate
2:00remaining
What happens if you pop the last view in NavigationStack?
In a NavigationStack with two views pushed, what happens when you pop the last pushed view?
iOS Swift
import SwiftUI

struct ContentView: View {
  @State private var path = [String]()

  var body: some View {
    NavigationStack(path: $path) {
      VStack {
        Button("Go to Detail") {
          path.append("Detail")
        }
      }
      .navigationDestination(for: String.self) { value in
        if value == "Detail" {
          Text("Detail Screen")
        }
      }
    }
  }
}
APopping the last view returns the user to the root view showing the button "Go to Detail".
BPopping the last view closes the app automatically.
CPopping the last view causes a runtime error because the path becomes empty.
DPopping the last view does nothing; the user stays on the same screen.
Attempts:
2 left
💡 Hint
Think about how NavigationStack manages the path array and what an empty path means.
lifecycle
advanced
2:00remaining
When is the navigationDestination modifier called in NavigationStack?
In SwiftUI's NavigationStack, when does the closure inside navigationDestination(for:) get executed?
iOS Swift
import SwiftUI

struct ContentView: View {
  @State private var path = [Int]()

  var body: some View {
    NavigationStack(path: $path) {
      Button("Go to Number 5") {
        path.append(5)
      }
      .navigationDestination(for: Int.self) { number in
        Text("Number: \(number)")
      }
    }
  }
}
AThe closure runs only once when the app launches and never again.
BThe closure runs immediately when the view appears, regardless of navigation.
CThe closure runs only when the path contains a new Int value and the view needs to display the destination for that value.
DThe closure runs every time the user taps anywhere on the screen.
Attempts:
2 left
💡 Hint
Consider when SwiftUI needs to show a new screen based on the navigation path.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly uses NavigationStack with a path binding?
Choose the correct SwiftUI code that uses NavigationStack with a @State path binding and navigationDestination modifier.
A
struct ContentView: View {
  @State private var path = [String]()
  var body: some View {
    NavigationStack(path: $path) {
      Button("Go") { path.append("Next") }
    }
    .navigationDestination(for: String.self) { Text($0) }
  }
}
B
struct ContentView: View {
  @State private var path = [String]()
  var body: some View {
    NavigationStack(path: $path) {
      Button("Go") { path.append("Next") }
      .navigationDestination(for: String.self) { Text($0) }
    }
  }
}
C
struct ContentView: View {
  @State var path = [String]()
  var body: some View {
    NavigationStack(path: path) {
      Button("Go") { path.append("Next") }
      .navigationDestination(for: String.self) { Text($0) }
    }
  }
}
D
struct ContentView: View {
  @State private var path = [String]()
  var body: some View {
    NavigationStack {
      Button("Go") { path.append("Next") }
      .navigationDestination(for: String.self) { Text($0) }
    }
  }
}
Attempts:
2 left
💡 Hint
Remember that NavigationStack's path parameter requires a binding and navigationDestination must be inside the NavigationStack closure.
🔧 Debug
expert
3:00remaining
Why does this NavigationStack code not navigate on button tap?
This SwiftUI code does not navigate to the detail view when the button is tapped. What is the cause?
iOS Swift
import SwiftUI

struct ContentView: View {
  @State private var path = [String]()

  var body: some View {
    NavigationStack {
      Button("Go to Detail") {
        path.append("Detail")
      }
      .navigationDestination(for: String.self) { value in
        Text("Detail Screen")
      }
    }
  }
}
AThe NavigationStack is missing the path binding; it should be NavigationStack(path: $path) to track navigation state.
BThe navigationDestination modifier is incorrectly placed outside the NavigationStack closure.
CThe path array must be declared as @Binding, not @State.
DThe Button label must be a NavigationLink for navigation to work.
Attempts:
2 left
💡 Hint
Check how NavigationStack tracks the navigation path and how the path state is connected.