Bird
0
0

You wrote this code to pop to the root view controller:

medium📝 Debug Q14 of 15
iOS Swift - Navigation
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?
Aanimated parameter must be false to avoid crashes.
BpopToRootViewController requires a completion handler parameter.
CnavigationController is nil because the view controller is not inside a navigation stack.
DYou must call popViewController instead of popToRootViewController.
Step-by-Step Solution
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]
Quick Trick: Check if navigationController is nil before popping [OK]
Common Mistakes:
  • Assuming popToRootViewController needs completion handler
  • Thinking animated must be false
  • Confusing popViewController with popToRootViewController

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes