Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
iOS Swift - Navigation
Consider this code:
let nav = UINavigationController(rootViewController: HomeViewController())
nav.pushViewController(SettingsViewController(), animated: true)
nav.popToRootViewController(animated: true)

What is the visible screen after these calls?
ASettingsViewController screen
BApp crashes due to incorrect navigation
CEmpty screen because stack is cleared
DHomeViewController screen
Step-by-Step Solution
Solution:
  1. Step 1: Understand pushViewController effect

    SettingsViewController is pushed on top of HomeViewController.
  2. Step 2: Understand popToRootViewController effect

    This method pops all view controllers except the root, showing HomeViewController again.
  3. Final Answer:

    HomeViewController screen -> Option D
  4. Quick Check:

    popToRootViewController shows root VC [OK]
Quick Trick: popToRootViewController returns to root screen [OK]
Common Mistakes:
  • Assuming popToRootViewController removes all VCs
  • Thinking SettingsViewController remains visible
  • Confusing popToRoot with popViewController

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes