Bird
0
0

Why is it important to check if navigationController is nil before pushing a view controller programmatically?

hard📝 Conceptual Q10 of 15
iOS Swift - Navigation
Why is it important to check if navigationController is nil before pushing a view controller programmatically?
ABecause navigationController manages tab bar items
BBecause pushing without a navigation controller causes a crash
CBecause pushViewController only works with modal presentations
DBecause navigationController is always nil in Swift
Step-by-Step Solution
Solution:
  1. Step 1: Understand navigationController optionality

    Not all view controllers are embedded in a navigation controller, so it can be nil.
  2. Step 2: Consequence of nil navigationController

    Calling pushViewController on nil causes a crash; checking prevents this.
  3. Final Answer:

    Because pushing without a navigation controller causes a crash -> Option B
  4. Quick Check:

    Check navigationController nil to avoid crash [OK]
Quick Trick: Always check navigationController is not nil before pushing [OK]
Common Mistakes:
  • Assuming navigationController is never nil
  • Confusing navigationController with tab bar controller
  • Thinking pushViewController works without navigation controller

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes