Work with Unwind Segue in Swift Language – iOS 8

Unwind - Exit
Unwind – Exit

An unwind segue (sometimes called exit segue) can be used to navigate back through push, modal or popover segues (as if you popped the navigation item from the navigation bar, closed the popover or dismissed the modally presented view controller). On top of that you can actually unwind through not only one but a series of push/modal/popover segues, e.g. “go back” multiple steps in your navigation hierarchy with a single unwind action.

To enable the Unwind Segue you need to add some code first.

@IBAction func unwindToVC(segue: UIStoryboardSegue) {
}

You have to add this code in the view controller where you want to unwind (came back).

Unwind To View
Unwind To View

Suppose you want to came back from second or third view to first view then you have to add the code in first view.

Then just connect any control(Which contains action) with the unwind segue.

Unwind Segue
Unwind Segue

For check the particular view controller by unwind segue, use following code :

    @IBAction func unwindToVC(segue:UIStoryboardSegue) {
        if(segue.sourceViewController .isKindOfClass(ViewController2))
        {
            let alert = UIAlertView()
            alert.title = "UnwindSegue"
            alert.message = "Unwind from view 2"
            alert.addButtonWithTitle("Ok")
            alert.show()
        }
        if(segue.sourceViewController .isKindOfClass(ViewController3))
        {
            let alert = UIAlertView()
            alert.title = "UnwindSegue"
            alert.message = "Unwind from view 3"
            alert.addButtonWithTitle("Ok")
            alert.show()
        }
    }

Download project with this stuff UnwindSegue.zip

In next tutorial you will find the topic Passing data with Unwind Segue in Swift Language – iOS 8

Thanks!

Happy Coding 😀

  • neb

    Thank you so much. You saved a life.

  • Heri

    Thanks a lot ^^

  • Isra Hun Ikal

    Hey bro!! nice n sweet tutorial here. I have a question and i would really appreciate if you can answer my problem. I want to implement this but not using a UIControl, i want to use the default back button that appears once you advance in the stack of the navigation controller. The reason i want to do this is cause i want to implement an animation. Thank you!