I need to pop a UIViewController from the navigation controller.
Just writing this line of code but taking an exception;
unexpectedly found nil while unwrapping an Optional value
self.navigationController.popViewControllerAnimated(true)
If I make the navigation controller optional, this line makes no effect, no popping
self.navigationController?.popViewControllerAnimated(true)
How to solve it?
You can do it by selecting the View Controller in Storyboard editor and clicking Editor -> Embed In -> Navigation Controller. Also make sure that you have your Storyboard Entry Point (the arrow that indicates which view controller is presented first) either pointing to Navigation Controller or before it.
There's also a setViewControllers(_:animated:) to include the pop animation. Alternatively you could find the second last view controller in the viewControllers array and then use popToViewController to avoid overwriting the entire view controller stack.
When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it.
You need to unwrap your navigationController correctly
if let navController = self.navigationController { navController.popViewController(animated: true) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With