Bird
Raised Fist0
iOS Swiftmobile~5 mins

Programmatic navigation in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is programmatic navigation in iOS Swift?
Programmatic navigation means moving between screens (view controllers) using code instead of storyboard segues. It lets you control when and how to show new screens.
Click to reveal answer
beginner
Which method is used to push a new view controller onto the navigation stack?
You use navigationController?.pushViewController(_:animated:) to show a new screen by adding it on top of the current one.
Click to reveal answer
beginner
How do you present a view controller modally in Swift?
Use present(_:animated:completion:) on the current view controller to show another screen that covers it, like a popup or full screen.
Click to reveal answer
intermediate
What is the difference between pushing and presenting a view controller?
Pushing adds a screen to the navigation stack with a back button. Presenting shows a screen modally without a back button, often for temporary tasks.
Click to reveal answer
intermediate
Why is it important to check if navigationController is not nil before pushing?
Because pushing only works if the current view controller is inside a navigation controller. If nil, pushing will fail and crash the app.
Click to reveal answer
Which method do you use to navigate to a new screen by adding it to the navigation stack?
ApopViewController(animated:)
Bpresent(_:animated:completion:)
CpushViewController(_:animated:)
Ddismiss(animated:completion:)
What happens when you call present(_:animated:completion:)?
AIt pushes a new screen with a back button.
BIt shows a new screen modally over the current one.
CIt removes the current screen.
DIt reloads the current screen.
Before pushing a view controller, you should check if:
AThe app is in background.
BThe view controller is nil.
CThe view controller is already visible.
DThe navigationController property is not nil.
Which navigation style provides a back button automatically?
APushing on navigation stack
BModal presentation
CPresenting a popover
DReplacing root view controller
To close a modally presented view controller, you use:
Adismiss(animated:completion:)
BpushViewController(_:animated:)
CpopViewController(animated:)
Dpresent(_:animated:completion:)
Explain how to navigate from one screen to another programmatically in iOS using Swift.
Think about the two main ways: pushing on navigation stack and presenting modally.
You got /4 concepts.
    Describe the difference between pushing a view controller and presenting it modally.
    Consider how the user returns to the previous screen in each case.
    You got /4 concepts.

      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