Bird
Raised Fist0
iOS Swiftmobile~20 mins

Programmatic navigation in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What happens when this SwiftUI button is tapped?
Consider this SwiftUI code snippet. What is the visible result when the button is tapped?
iOS Swift
struct ContentView: View {
  @State private var isDetailActive = false
  var body: some View {
    NavigationView {
      VStack {
        NavigationLink(destination: Text("Detail View"), isActive: $isDetailActive) {
          EmptyView()
        }
        Button("Go to Detail") {
          isDetailActive = true
        }
      }
    }
  }
}
AThe app navigates to a new screen showing "Detail View" text.
BNothing happens because NavigationLink is hidden.
CThe button causes a crash due to missing NavigationLink label.
DThe button toggles a boolean but does not navigate.
Attempts:
2 left
💡 Hint
Think about how NavigationLink with isActive binding works in SwiftUI.
❓ lifecycle
intermediate
2:00remaining
What is the effect of calling pushViewController in UIKit?
In UIKit, what happens when you call navigationController?.pushViewController(newVC, animated: true)?
AThe newVC is presented modally over the current view.
BThe current view controller is replaced without animation.
CThe newVC is added on top of the navigation stack and shown with animation.
DThe app crashes because pushViewController requires a storyboard.
Attempts:
2 left
💡 Hint
Think about how navigation stacks work in UIKit.
📝 Syntax
advanced
2:00remaining
Which Swift code correctly performs programmatic navigation in SwiftUI?
Select the code snippet that correctly navigates to DetailView when a button is pressed.
iOS Swift
struct ContentView: View {
  @State private var showDetail = false
  var body: some View {
    NavigationView {
      VStack {
        Button("Show Detail") {
          showDetail = true
        }
        // NavigationLink here
      }
    }
  }
}
ANavigationLink(destination: DetailView(), isActive: showDetail) { Text("Go") }
BNavigationLink(destination: DetailView(), isActive: $showDetail) { EmptyView() }
CNavigationLink(isActive: showDetail, destination: DetailView()) { EmptyView() }
DNavigationLink(destination: DetailView()) { Button("Show Detail") { showDetail = true } }
Attempts:
2 left
💡 Hint
Check the correct use of isActive binding in NavigationLink.
🔧 Debug
advanced
2:00remaining
Why does this UIKit navigation code fail to show the new screen?
Given this code snippet, why does tapping the button not navigate to NewViewController? class ViewController: UIViewController { @IBAction func buttonTapped(_ sender: UIButton) { let newVC = NewViewController() newVC.modalPresentationStyle = .fullScreen } }
ABecause modalPresentationStyle must be set after presenting.
BBecause buttonTapped is missing @objc attribute.
CBecause NewViewController must be loaded from storyboard.
DBecause newVC is created but never pushed or presented.
Attempts:
2 left
💡 Hint
Check if the new view controller is actually shown.
🧠 Conceptual
expert
2:00remaining
What is the main difference between programmatic navigation and declarative navigation in iOS?
Choose the best explanation of the difference between programmatic navigation (UIKit) and declarative navigation (SwiftUI).
AProgrammatic navigation uses explicit commands to push or present views, while declarative navigation uses state bindings to describe navigation paths.
BProgrammatic navigation requires storyboards, declarative navigation does not.
CDeclarative navigation is only for modal views, programmatic is for push navigation.
DProgrammatic navigation is faster at runtime than declarative navigation.
Attempts:
2 left
💡 Hint
Think about how navigation is triggered in UIKit vs SwiftUI.

Practice

(1/5)
1. What does pushViewController do in iOS programmatic navigation?
easy
A. It moves to a new screen by adding it on top of the navigation stack.
B. It removes the current screen from the navigation stack.
C. It resets the entire navigation stack to the root screen.
D. It presents a screen modally without navigation.

Solution

  1. Step 1: Understand navigation stack behavior

    In iOS, navigation uses a stack where screens are pushed and popped.
  2. Step 2: Role of pushViewController

    The method pushViewController adds a new screen on top, moving forward.
  3. Final Answer:

    It moves to a new screen by adding it on top of the navigation stack. -> Option A
  4. Quick Check:

    pushViewController = move forward [OK]
Hint: Push adds a screen on top; pop removes the top screen [OK]
Common Mistakes:
  • Confusing push with pop behavior
  • Thinking push resets the stack
  • Mixing modal presentation with push navigation
2. Which of the following is the correct syntax to push a view controller named DetailViewController onto the navigation stack?
easy
A. navigationController.pushViewController(DetailViewController, animated: true)
B. navigationController?.pushViewController(DetailViewController(), animated: true)
C. navigationController.push(DetailViewController(), animated: true)
D. navigationController?.pushViewController(DetailViewController, animated: false)

Solution

  1. Step 1: Check method call and optional chaining

    navigationController is optional, so use ? to safely call method.
  2. Step 2: Instantiate the view controller correctly

    Use DetailViewController() to create an instance before pushing.
  3. Final Answer:

    navigationController?.pushViewController(DetailViewController(), animated: true) -> Option B
  4. Quick Check:

    Optional chaining + instance creation = correct syntax [OK]
Hint: Use ? for optional navigationController and () to create instance [OK]
Common Mistakes:
  • Forgetting to instantiate the view controller
  • Not using optional chaining on navigationController
  • Using wrong method name like push instead of pushViewController
3. Given this code snippet, what will happen when the button is tapped?
func buttonTapped() {
  let vc = SettingsViewController()
  navigationController?.pushViewController(vc, animated: true)
}
medium
A. The app will crash because SettingsViewController is not initialized properly.
B. Nothing will happen because pushViewController is called without a navigation controller.
C. The current screen will be removed and replaced by SettingsViewController.
D. The app will navigate to the SettingsViewController screen with animation.

Solution

  1. Step 1: Analyze the code behavior

    The code creates a new SettingsViewController instance and pushes it onto the navigation stack.
  2. Step 2: Understand navigationController optional chaining

    If navigationController exists, it will push with animation, moving to the new screen.
  3. Final Answer:

    The app will navigate to the SettingsViewController screen with animation. -> Option D
  4. Quick Check:

    pushViewController with valid instance = navigate forward [OK]
Hint: pushViewController moves forward if navigationController exists [OK]
Common Mistakes:
  • Assuming crash without checking optional navigationController
  • Thinking push removes current screen
  • Confusing push with modal presentation
4. Identify the error in this code snippet for navigating back to the previous screen:
func goBack() {
  navigationController.popViewController(animated: true)
}
medium
A. popViewController is not a valid method for navigationController.
B. popViewController should be called with a completion handler.
C. Missing optional chaining operator (?) after navigationController.
D. Missing parentheses after navigationController to call optional chaining.

Solution

  1. Step 1: Check navigationController usage

    navigationController is optional, so it needs optional chaining with ? before calling methods.
  2. Step 2: Identify missing ? operator

    The code uses navigationController.popViewController without ?, causing a compile error.
  3. Final Answer:

    Missing optional chaining operator (?) after navigationController. -> Option C
  4. Quick Check:

    Optional chaining needed for navigationController calls [OK]
Hint: Use ? after navigationController to safely call methods [OK]
Common Mistakes:
  • Forgetting navigationController is optional
  • Trying to add completion handler to popViewController
  • Confusing popViewController with pushViewController
5. You want to programmatically navigate back to the root view controller from a deep screen. Which code snippet correctly achieves this?
hard
A. navigationController?.popToRootViewController(animated: true)
B. navigationController?.popViewController(animated: true)
C. navigationController?.dismiss(animated: true)
D. navigationController?.pushViewController(rootViewController, animated: true)

Solution

  1. Step 1: Understand navigation stack methods

    popToRootViewController moves back to the first screen in the stack, popping all intermediate ones.
  2. Step 2: Compare options

    popViewController removes only the top screen; dismiss is for modal; push adds a new screen, not back.
  3. Final Answer:

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

    popToRootViewController = go back to root [OK]
Hint: Use popToRootViewController to return to first screen [OK]
Common Mistakes:
  • Using popViewController to go back multiple screens
  • Confusing dismiss with navigation pop
  • Trying to push root instead of popping to it
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