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?


Solution 1:

You need to unwrap your navigationController correctly

if let navController = self.navigationController {
    navController.popViewController(animated: true)
}

Solution 2:

Swift 3.0 This is working for me

self.navigationController?.popViewController(animated: true)

enter image description here