Bird
0
0

Given this code snippet, what will happen when the button is tapped?

medium📝 ui behavior Q13 of 15
iOS Swift - Navigation
Given this code snippet, what will happen when the button is tapped?
func buttonTapped() {
  let vc = SettingsViewController()
  navigationController?.pushViewController(vc, animated: true)
}
AThe app will crash because SettingsViewController is not initialized properly.
BNothing will happen because pushViewController is called without a navigation controller.
CThe current screen will be removed and replaced by SettingsViewController.
DThe app will navigate to the SettingsViewController screen with animation.
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes