15

How to return back to previous view controller programmatically? I found this answer, but there is an example that demonstrate how to go back if we have navigation stack:

navigationController?.popViewControllerAnimated(true)

It's ok in case my queue of controllers based on navigation controller. But usually we use storyboard where we specify segue that marked with keyword Show that means we don't care about navigation push or present new view controllers. So in this case I presume there is only option with unwind view controller via segue, but maybe there is some simple call that I can do programmatically to go back to my previous view controller without checking if my stack of view controllers contain UINavigationController or not.

I am looking for something simple like self.performSegueToReturnBack.

Community
  • 1
  • 1
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • read about unwindSegue – Lu_ Aug 03 '16 at 10:54
  • Hi @Lu_ yea I mention about it in my question, I know about this approach. But it requires additional setup in Storyboard. This is not an programatic approach. But of course this is a solution. Agree with you. – Matrosov Oleksandr Aug 03 '16 at 10:55
  • so you cannot make anything without any preparation, some effort have to be done :) if you know about segues you can make segue back like you did forward – Lu_ Aug 03 '16 at 10:57
  • @Lu_ if you see this topic: https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/ we connect some segue to exit. But I created my button programmatically and there is no option for me to unwind. – Matrosov Oleksandr Aug 03 '16 at 10:59

2 Answers2

45

You can easily extend functionality of any inbuilt classes or any other classes through extensions. This is the perfect use cases of extensions in swift.

You can make extension of UIViewController like this and use the performSegueToReturnBack function in any UIViewController

Swift 2.0

extension UIViewController {
    func performSegueToReturnBack()  {
        if let nav = self.navigationController {
            nav.popViewControllerAnimated(true)
        } else {
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }
}

Swift 3.0

extension UIViewController {
    func performSegueToReturnBack()  {
        if let nav = self.navigationController {
            nav.popViewController(animated: true)
        } else {
            self.dismiss(animated: true, completion: nil)
        }
    }
}

Note:

Someone suggested that we should assign _ = nav.popViewControllerAnimated(true) to an unnamed variable as compiler complains if we use it without assigning to anything. But I didn't find it so.

Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63
  • yea that's what I expected to see in iOS SDK =) but I guess there is no appropriate method there. Thanks I guess this is a solution just for now. – Matrosov Oleksandr Aug 03 '16 at 11:02
  • This didn't work for me, since I had modally presented view controllers that returned a navigation controller - and needed to be dismissed, not popped. It failed silently. – AmitaiB Mar 03 '17 at 01:27
  • So you are trying to dismiss a navigation controller from first view controller i assume. You can do self.navigationController.performsegueToReturenBack() in that case. – Prajeet Shrestha Mar 03 '17 at 02:04
  • If the OP needs an 'agnostic' way to return to the previous view controller, then we'd need to detect how our VC is presented: modally/shown. If so, `navigationController != nil` != presenting modally, since acc. to the [docs](https://developer.apple.com/reference/uikit/uiviewcontroller/1621860-navigationcontroller#) it returns: "_The nearest ancestor in the view controller hierarchy that is a navigation controller._" If NavVC → ParentVC presents modally programmatically, then `navigationController` returns `.Some` even though it is modal and requires `dismiss`, not `pop`. – AmitaiB Mar 03 '17 at 16:49
  • If u can manage to share diagram of your controller hierarchy then it would clarify things up. – Prajeet Shrestha Mar 03 '17 at 17:11
4

Best answer is this: _ = navigationController?.popViewController(animated: true)

Taken from here: https://stackoverflow.com/a/28761084/2173368

Giggs
  • 851
  • 10
  • 16