Bird
Raised Fist0
iOS Swiftmobile~10 mins

Programmatic navigation in iOS Swift - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to push a new view controller onto the navigation stack.

iOS Swift
navigationController?.[1](DetailViewController(), animated: true)
Drag options to blanks, or click blank then click option'
Apresent
BpushViewController
CpopViewController
Ddismiss
Attempts:
3 left
💡 Hint
Common Mistakes
Using popViewController instead of pushViewController
Using present instead of pushViewController
2fill in blank
medium

Complete the code to pop the current view controller from the navigation stack.

iOS Swift
navigationController?.[1](animated: true)
Drag options to blanks, or click blank then click option'
ApushViewController
Bpresent
CpopViewController
Ddismiss
Attempts:
3 left
💡 Hint
Common Mistakes
Using pushViewController instead of popViewController
Using dismiss instead of popViewController
3fill in blank
hard

Fix the error in the code to present a view controller modally.

iOS Swift
present([1], animated: true, completion: nil)
Drag options to blanks, or click blank then click option'
ADetailViewController()
Bself
CnavigationController
Dself.navigationController
Attempts:
3 left
💡 Hint
Common Mistakes
Passing self instead of the new view controller
Passing navigationController instead of the new view controller
4fill in blank
hard

Fill both blanks to pop to the root view controller with animation.

iOS Swift
navigationController?.[1](animated: [2])
Drag options to blanks, or click blank then click option'
ApopToRootViewController
Btrue
Cfalse
DpopViewController
Attempts:
3 left
💡 Hint
Common Mistakes
Using popViewController instead of popToRootViewController
Setting animated to false when animation is desired
5fill in blank
hard

Fill all three blanks to replace the current view controller stack with a new one.

iOS Swift
navigationController?.[1]([[2]], animated: [3])
Drag options to blanks, or click blank then click option'
AsetViewControllers
BDetailViewController()
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using pushViewController instead of setViewControllers
Passing a single view controller instead of an array
Setting animated to false when animation is desired

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