Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popping ViewController on Swift

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?

like image 622
erdemgc Avatar asked Aug 20 '14 08:08

erdemgc


People also ask

How do I pop navigation controller in Swift?

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.

How do I pop two viewControllers at a time in Swift?

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.

How do I dismiss a view controller?

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.


1 Answers

You need to unwrap your navigationController correctly

if let navController = self.navigationController {     navController.popViewController(animated: true) } 
like image 72
karlofk Avatar answered Oct 08 '22 13:10

karlofk