Featured Post

Applying Email Validation to a JavaFX TextField Using Binding

This example uses the same controller as in a previous post but adds a use case to support email validation.  A Commons Validator object is ...

Thursday, September 24, 2015

The Unwind Segue in Xcode 7 and Swift

If you use a UINavigationController, moving from UIViewController to UIViewController is handled automatically.  However, if you are providing your own navigation, then you can use a Show Segue and an Unwind Segue to switch UIViewControllers. This example hooks a Swipe Gesture Recognizer to a Show Segue and a second Recognizer to an Unwind Segue to restore the original view.

If you're into JavaFX, see how this Apple-oriented post compares with this video.

The following video shows an iPod running the app on iOS 9.  Swiping down on the Main Content Screen brings up the Additional Content Screen.  Swiping up on the Additional Content Screen returns the Main Content Screen.


Two UIViewControllers are added to an Xcode 7 project (one was created by Xcode based on the Single View Application project type).  Each UIViewControllers has a UILabel centered in the UIView and the background of Main Content Here is set to blue.

Single View Application with Two UIViewControllers
Step 1 - Connect a Swipe Gesture Recognizer to a Show Segue

The first step will add a down Swipe Gesture Recognizer to the Main Content VC.  From the toolkit in the lower right hand of the screen, drag Swipe Gesture Recognizer to Main Content VC.  This will add a fourth icon to the top of the VC (a blueish white box to the left of the exit button).

Drag Swipe Gesture Recognizer to Main Content VC
Select the Swipe Gesture Recognizer and select the Properties tab.  Set the Swipe direction to "Down".

Control + drag from the newly-added Swipe Gesture Recognizer to the Additional Content VC.  Define this as a Show segue.

At this point, the application will navigate from the Main Content VC to the Additional Content VC with a down swipe (but won't return yet).

Step 2 - Add Code

In the UIViewController subclass supporting Main Content VC (is "ViewController.swift" if you're using the XCode-generated VC), add the following code.  The actual name of the method is not important, only that it's marked as an @IBAction and takes a single UIStoryboardSegue argument.

Note that this code belongs in the unwind target which is the Main Content VC.  Even though we'll be hooking this up in the next step to Additional Content VC's Exit button, this code does not go into the UIViewController Swift file of Additional Content VC.

    @IBAction func unwindToMainContent(segue:UIStoryboardSegue) {
    }

Step 3 - Connect a Swipe Gesture Recognizer to the Unwind Segue

Similar to what was done in Step 1, add a Swipe Gesture Recognizer to Additional Content VC.  In the properties panel, set this Swipe direction to "Up".

At the top of the Additional Content VC, Control + Drag from the newly-added Swipe Gesture Recognizer icon to the Exit icon.  The method name added in Step 2 will appear ("unwindToMainContent" if you're using my name).  Select this.

Wiring a Swipe Gesture Recognizer to an Unwind Segue
Even without a UINavigationController, moving between UIViewControllers is simple in iOS and can be done with a minimum of code (one empty method).

No comments:

Post a Comment