iOS Swift - NavigationWhy does this code cause a crash? let newVC: UIViewController? = nil navigationController?.pushViewController(newVC!, animated: true) Aanimated parameter cannot be trueBYou cannot push a nil view controller onto the navigation stackCnavigationController must be force unwrappedDpushViewController requires a completion handlerCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand pushViewController parameterpushViewController requires a non-nil UIViewController instance.Step 2: Check consequences of nil argumentPassing nil (via force-unwrap newVC!) causes runtime crash because method expects a valid controller.Final Answer:You cannot push a nil view controller onto the navigation stack -> Option BQuick Check:pushViewController(nil) causes crash [OK]Quick Trick: Never push nil view controller; always instantiate first [OK]Common Mistakes:Passing nil instead of a valid view controllerThinking animated parameter causes crashBelieving force unwrap is required
Master "Navigation" in iOS Swift9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepUITryChallengeBuildRecallPublish
More iOS Swift Quizzes Animations - Implicit animations (.animation modifier) - Quiz 6medium Concurrency - Why async/await simplifies concurrent code - Quiz 13medium Lists and Data Display - Search with searchable modifier - Quiz 3easy Lists and Data Display - Section headers and footers - Quiz 1easy Lists and Data Display - List with ForEach - Quiz 6medium Local Data Persistence - Model definition with @Model - Quiz 13medium Navigation - TabView for tab navigation - Quiz 15hard Navigation - TabView for tab navigation - Quiz 8hard Networking - Error handling for network calls - Quiz 14medium Networking - GET request with async/await - Quiz 5medium