Giter VIP home page Giter VIP logo

unwind-segue-demo's Introduction

Unwind-Segue-Demo

Barebones demo of using unwind segues to prevent nasty navigation stacks

Also see Apple's documentation

Adding Unwind Segues

Declare The Segue

In each UIViewController subclass that should support unwind segues(I've used the RedViewController here), an IBAction like the following should be added:

@IBAction func unwindToRedViewController(segue: UIStoryboardSegue) {}

The action doesn't need to do anything, it's just there to serve as an access point for storyboards and such.

Connect The Segue

To make the unwind segue available to use programmatically by a view controller, control+click on the view controller icon in your scene, and connect it to the exit.

![](images/Screen Shot 2016-06-09 at 11.05.18 AM.png)

Choose the unwind segue from the menu

![](images/Screen Shot 2016-06-09 at 11.05.27 AM.png)

Make sure to give the segue an identifier.

![](images/Screen Shot 2016-06-09 at 11.11.28 AM.png)

![](images/Screen Shot 2016-06-09 at 11.11.40 AM.png)

Choose Your Segue

In this storyboard, we have two buttons with normal show segues going to two other view controllers.

![](images/Screen Shot 2016-06-09 at 10.54.30 AM.png)

In order to choose between segues later, each segue has an identifier, just like the unwind segues.

![](images/Screen Shot 2016-06-09 at 11.00.45 AM.png)

![](images/Screen Shot 2016-06-09 at 11.02.43 AM.png)

If we don't want to push an additional destination view controller onto the navigation stack if one of it's kind already exists, we can decide to use the unwind segue instead by implementing shouldPerformSegueWithIdentifier(_:sender:) in our view controller

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
    guard let navigationController = navigationController else { return true }

    if identifier == "PushGreen" && navigationController.childViewControllers.filter({ $0.isKindOfClass(GreenViewController) }).count > 0 {
        performSegueWithIdentifier("unwindGreen", sender: self)
        return false
    } else if identifier == "PushBlue" && navigationController.childViewControllers.filter({ $0.isKindOfClass(BlueViewController) }).count > 0 {
        performSegueWithIdentifier("unwindBlue", sender: self)
        return false
    }

    return true
}

Now, if we are attempting to show a view controller via the show segue and one of those view controllers is already in the navigation stack, we will pop to it instead.

unwind-segue-demo's People

Contributors

jesse-calkin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.