Bird
0
0

Given this code snippet, what will happen when navigate() is called?

medium📝 Predict Output Q4 of 15
iOS Swift - Navigation
Given this code snippet, what will happen when navigate() is called?
func navigate() {
  let vc = UIViewController()
  vc.view.backgroundColor = .red
  navigationController?.pushViewController(vc, animated: false)
}
ANothing happens because animation is false
BA new red screen appears without animation
CThe current screen is replaced with a red screen with animation
DApp crashes due to missing storyboard
Step-by-Step Solution
Solution:
  1. Step 1: Analyze pushViewController call

    The code creates a new UIViewController with red background and pushes it without animation.
  2. Step 2: Understand effect of animated: false

    Animation is disabled but the new screen still appears immediately.
  3. Final Answer:

    A new red screen appears without animation -> Option B
  4. Quick Check:

    pushViewController with animated false = immediate screen change [OK]
Quick Trick: animated false means no animation but screen still changes [OK]
Common Mistakes:
  • Thinking animated false prevents navigation
  • Assuming push replaces current screen instead of adding
  • Believing storyboard is required for programmatic VC

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes