Bird
Raised Fist0
iOS Swiftmobile~20 mins

Navigation path management in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Navigation Path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ navigation
intermediate
2:00remaining
What is the output of this SwiftUI navigation code?
Consider this SwiftUI code snippet that manages navigation paths. What will be the value of navigationPath after tapping the button once?
iOS Swift
import SwiftUI

struct ContentView: View {
  @State private var navigationPath = NavigationPath()

  var body: some View {
    NavigationStack(path: $navigationPath) {
      VStack {
        Button("Go to Detail") {
          navigationPath.append("DetailView")
        }
        Text("Current path count: \(navigationPath.count)")
      }
      .navigationDestination(for: String.self) { value in
        Text("You are at \(value)")
      }
    }
  }
}
AnavigationPath contains one element: ["DetailView"]
BnavigationPath is empty: []
CnavigationPath contains two elements: ["DetailView", "DetailView"]
DnavigationPath contains one element: ["ContentView"]
Attempts:
2 left
💡 Hint
Think about what happens when you append a string to the navigation path once.
❓ ui_behavior
intermediate
1:30remaining
Which option correctly resets the navigation path in SwiftUI?
You want to reset the navigation stack to the root view by clearing the navigation path. Which code snippet correctly does this?
AnavigationPath.clear()
BnavigationPath = nil
CnavigationPath = []
DnavigationPath.removeAll()
Attempts:
2 left
💡 Hint
Check the NavigationPath API for clearing all elements.
❓ lifecycle
advanced
2:00remaining
What happens to the navigation path when the view is recreated?
Given this SwiftUI view with a @State navigationPath, what happens to the navigation path when the view is recreated due to a state change elsewhere?
iOS Swift
struct ContentView: View {
  @State private var navigationPath = NavigationPath()
  @State private var counter = 0

  var body: some View {
    NavigationStack(path: $navigationPath) {
      VStack {
        Button("Go to Detail") {
          navigationPath.append("DetailView")
        }
        Button("Increment") {
          counter += 1
        }
        Text("Counter: \(counter)")
      }
      .navigationDestination(for: String.self) { value in
        Text("You are at \(value)")
      }
    }
  }
}
AThe navigation path resets to empty every time the view updates.
BThe navigation path is preserved and remains unchanged.
CThe navigation path duplicates its elements on each update.
DThe navigation path causes a runtime error on view update.
Attempts:
2 left
💡 Hint
Remember how @State preserves data across view updates.
📝 Syntax
advanced
2:00remaining
Which option causes a compile-time error in navigation path usage?
Which of these SwiftUI code snippets will cause a compile-time error when used with NavigationStack's path binding?
A
@State private var navigationPath: [String] = []

NavigationStack(path: $navigationPath) { ... }
B
@State private var navigationPath = NavigationPath()

NavigationStack(path: $navigationPath) { ... }
C
@State private var navigationPath = NavigationPath()

NavigationStack(path: navigationPath) { ... }
D
@State private var navigationPath = NavigationPath()

NavigationStack(path: Binding(get: { navigationPath }, set: { navigationPath = $0 })) { ... }
Attempts:
2 left
💡 Hint
Check if the path parameter requires a binding or a value.
🔧 Debug
expert
3:00remaining
Why does this navigation path code cause a runtime crash?
This SwiftUI code crashes at runtime when navigating. What is the cause?
iOS Swift
struct ContentView: View {
  @State private var navigationPath = NavigationPath()

  var body: some View {
    NavigationStack(path: $navigationPath) {
      VStack {
        Button("Go to Detail") {
          navigationPath.append(42)
        }
      }
      .navigationDestination(for: String.self) { value in
        Text("You are at \(value)")
      }
    }
  }
}
AMissing navigationDestination for Int type causes a crash.
BAppending an Int (42) to a NavigationPath expecting String causes a crash.
CNavigationPath cannot hold Int values, only Strings.
DThe Button action is not wrapped in DispatchQueue.main.async, causing a crash.
Attempts:
2 left
💡 Hint
Check the types used in append and navigationDestination.

Practice

(1/5)
1. What does the pushViewController(_:animated:) method do in a UINavigationController?
easy
A. It removes the current screen from the navigation stack.
B. It adds a new screen on top of the current navigation stack.
C. It replaces the entire navigation stack with a new screen.
D. It resets the navigation stack to the root screen.

Solution

  1. Step 1: Understand pushViewController behavior

    The method adds a new view controller on top of the current stack, showing a new screen.
  2. Step 2: Compare with other navigation actions

    Pop removes the top screen, replace resets stack, so push only adds on top.
  3. Final Answer:

    It adds a new screen on top of the current navigation stack. -> Option B
  4. Quick Check:

    pushViewController = add new screen [OK]
Hint: Push means add a new screen on top [OK]
Common Mistakes:
  • Confusing push with pop (removing screen)
  • Thinking push replaces the whole stack
  • Assuming push resets to root screen
2. Which of the following is the correct Swift syntax to pop the current view controller from a navigation stack?
easy
A. navigationController.popViewController(animated: true)
B. navigationController.pushViewController(animated: true)
C. navigationController.pop(animated: true)
D. navigationController.removeViewController(animated: true)

Solution

  1. Step 1: Recall the correct method name

    The method to remove the top view controller is popViewController(animated:).
  2. Step 2: Check method usage

    It is called on the navigationController instance with a Bool for animation.
  3. Final Answer:

    navigationController.popViewController(animated: true) -> Option A
  4. Quick Check:

    Pop method is popViewController(animated:) [OK]
Hint: Pop removes top screen, method ends with ViewController [OK]
Common Mistakes:
  • Using push instead of pop
  • Using incorrect method names like pop() or removeViewController()
  • Missing the animated parameter
3. Consider this Swift code snippet inside a view controller:
navigationController?.pushViewController(detailVC, animated: true)
navigationController?.popViewController(animated: true)
navigationController?.popViewController(animated: true)
If the navigation stack initially has 3 view controllers, how many view controllers remain after this code runs?
medium
A. 2
B. 1
C. 3
D. 0

Solution

  1. Step 1: Analyze push action

    Push adds 1 view controller, so stack goes from 3 to 4.
  2. Step 2: Analyze two pop actions

    Each pop removes 1 view controller, so 4 - 2 = 2 remain.
  3. Step 3: Confirm final count

    After two pops, 2 view controllers remain on the stack.
  4. Final Answer:

    2 -> Option A
  5. Quick Check:

    3 + 1 - 2 = 2 [OK]
Hint: Push adds 1, each pop removes 1 [OK]
Common Mistakes:
  • Forgetting push adds a controller
  • Counting only one pop instead of two
  • Assuming stack can't go below 3
4. You wrote this code to pop to the root view controller:
navigationController.popToRootViewController(animated: true)
But your app crashes with an error. What is the most likely cause?
medium
A. animated parameter must be false to avoid crashes.
B. popToRootViewController requires a completion handler parameter.
C. navigationController is nil because the view controller is not inside a navigation stack.
D. You must call popViewController instead of popToRootViewController.

Solution

  1. Step 1: Understand navigationController optional

    navigationController is optional and can be nil if the current controller is not inside a navigation stack.
  2. Step 2: Check method requirements

    popToRootViewController(animated:) does not require a completion handler and accepts true or false for animation.
  3. Final Answer:

    navigationController is nil because the view controller is not inside a navigation stack. -> Option C
  4. Quick Check:

    Nil navigationController causes crash [OK]
Hint: Check if navigationController is nil before popping [OK]
Common Mistakes:
  • Assuming popToRootViewController needs completion handler
  • Thinking animated must be false
  • Confusing popViewController with popToRootViewController
5. You want to replace the entire navigation stack with a new set of view controllers: [homeVC, profileVC, settingsVC]. Which code snippet correctly achieves this?
hard
A. navigationController.popViewController(animated: true)
B. navigationController.pushViewController(settingsVC, animated: true)
C. navigationController.popToRootViewController(animated: true)
D. navigationController.setViewControllers([homeVC, profileVC, settingsVC], animated: true)

Solution

  1. Step 1: Identify method to replace stack

    The method setViewControllers(_:animated:) replaces the entire navigation stack with the given array.
  2. Step 2: Compare other options

    Push adds one controller, popToRoot pops to root only, popViewController pops one controller.
  3. Final Answer:

    navigationController.setViewControllers([homeVC, profileVC, settingsVC], animated: true) -> Option D
  4. Quick Check:

    setViewControllers replaces stack [OK]
Hint: Use setViewControllers to replace whole stack [OK]
Common Mistakes:
  • Using push to add instead of replace
  • Using pop methods which only remove controllers
  • Forgetting to pass array of controllers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard