Confusing popViewController with popToRootViewController
5. You want to replace the entire navigation stack with a new set of view controllers: [homeVC, profileVC, settingsVC]. Which code snippet correctly achieves this?
hard
A. navigationController.popViewController(animated: true)
B. navigationController.pushViewController(settingsVC, animated: true)
C. navigationController.popToRootViewController(animated: true)
D. navigationController.setViewControllers([homeVC, profileVC, settingsVC], animated: true)
Solution
Step 1: Identify method to replace stack
The method setViewControllers(_:animated:) replaces the entire navigation stack with the given array.
Step 2: Compare other options
Push adds one controller, popToRoot pops to root only, popViewController pops one controller.
Final Answer:
navigationController.setViewControllers([homeVC, profileVC, settingsVC], animated: true) -> Option D
Quick Check:
setViewControllers replaces stack [OK]
Hint: Use setViewControllers to replace whole stack [OK]
Common Mistakes:
Using push to add instead of replace
Using pop methods which only remove controllers
Forgetting to pass array of controllers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.