Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS how to simple return back to previous presented/pushed view controller programmatically?

Tags:

ios

swift

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.

like image 741
Matrosov Oleksandr Avatar asked Aug 03 '16 10:08

Matrosov Oleksandr


People also ask

Should a view controller know about previous view controllers?

You might want communication to happen at any moment and not only when a transition occurs. In these cases, we need a direct connection between the current view controller and the previous one. As I explained above though view controllers should not know anything about previous ones. The solution is delegation.

How do I preserve my iOS app’s view controllers?

The preservation and restoration process is mostly automatic, but you need to tell iOS which parts of your app to preserve. The steps for preserving your app’s view controllers are as follows: (Required) Assign restoration identifiers to the view controllers whose configuration you want to preserve; see Tagging View Controllers for Preservation.

How do the view controllers update the state of the app?

The first view controller updates the state of the app as a consequence of user interaction. When the second view controller comes on screen, it fetches the updated data from the model controller and updates its interface.

How do i pop a view controller from another controller?

Instead of using the navigation controller to pop a view controller, use unwind segues. This solution has a few, but really important, advantages: The origin controller can go back to any other destination controller (not just the previous one) without knowing anything about the destination.


2 Answers

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.

like image 85
Prajeet Shrestha Avatar answered Sep 29 '22 10:09

Prajeet Shrestha


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

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

like image 36
Giggs Avatar answered Sep 29 '22 11:09

Giggs