Bird
0
0

You want to replace the entire navigation stack with a new root view controller called HomeViewController. Which code snippet achieves this correctly?

hard📝 Application Q8 of 15
iOS Swift - Navigation
You want to replace the entire navigation stack with a new root view controller called HomeViewController. Which code snippet achieves this correctly?
AnavigationController?.popViewController(animated: true)
BnavigationController?.pushViewController(HomeViewController(), animated: true)
CnavigationController?.setViewControllers([HomeViewController()], animated: true)
DnavigationController?.popToRootViewController(animated: true)
Step-by-Step Solution
Solution:
  1. Step 1: Understand setViewControllers method

    This method replaces the entire stack with the provided array of view controllers.
  2. Step 2: Compare with other methods

    pushViewController adds one controller, popToRootViewController goes back to existing root, popViewController pops one controller.
  3. Final Answer:

    navigationController?.setViewControllers([HomeViewController()], animated: true) -> Option C
  4. Quick Check:

    Replace stack = setViewControllers with array [OK]
Quick Trick: Use setViewControllers to replace entire stack [OK]
Common Mistakes:
  • Using push to replace stack
  • Confusing popToRoot with replacing stack
  • Trying to pop single controller only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes