Blog

  • Segues are very common to use, using it we can move to next screen. But there is special kind of segue which helps us to travel backward on the screen called “unwind segue”. An unwind segue is a segue that enables the user to navigate back from their current view controller to the previous view controller. Unwind segue is also called “Exit Segue”. An unwind segue can be utilized to navigate back through push, modal or popover segues


    Below are steps to perform unwind segue:

    1) Define an IBaction unwindTo_ :

    Firstly we have to create an ibaction that we can wire up unwind segue to.
    Suppose we have two view controllers - MainViewController and LoginViewController and we have to navigate back to LoginViewController from MainViewController then define the Inaction in MainViewController.

     

    class  LoginViewController: UIViewController {
    @IBAction func unwindTo LoginViewController(segue: UIStoryboard) { }
    }
    

     

    2) Wire up the unwind segue:

    To wire up the MainViewController to its unwind segue, we have to right-click and drag from the view controller icon to exit icon in the storyboard scene.When we drag, we will be presented with a list of IBActions to connect to. Then we will choose the unwind segue action that we created above.

     

    3) Specify a Segue Identifier in the storyboard :

    For this, we have to check the document outline of the storyboard. As from document Outline, we can easily select the unwind segue in the outline and specify its identifier the attributes Inspector of the utility Pane.

     

    4) Trigger Unwind segue programmatically :

    The last main step is to trigger the segue from the controller you want to go back. Like in this case we will trigger the unwind segue from MainViewController

    self.performSegue(with identifier: “unwindTo LoginView” , sender: self)
    
    

    Here above we have to use the same identifier that we have defined in the third step in the storyboard.

    Thanks.

     

     

     

Tags: swift