iOS 6 Storyboard Unwind Scene

Source

(c) 2012 kevin languedoc (klanguedoc)

View Controllers (UIViewController) are the main object to use when creating UIs (User Interfaces) for iOS apps for iPhones, iPads and iPod Touches. As apps get more sophisticated and developers add more and more View Controllers and scenes, managing this huge array of UIViewControllers can become a nightmare and you could see your app running out of memory and either crashing the app or both the app and the device. Apple has recognized this problem with the previous version of the iOS SDK and has introduced a new feature to help developers create iOS applications with multiple UIViewControllers or another other view controller like the UITableViewController to reuse the various view controllers rather than creating new ones every time a scene is loaded on top of the stack.

This tutorial will demonstrate the new unwind feature that is available in the storyboard. This feature is not available for Xibs. The tutorial is demonstrate how to set up a couple of view controllers in a storyboard and how to configure the scenes to use the new Exit object and add code to the view controllers to implement the Exit or unWind feature.

Project Definition

The first task is to create an iOS project. For this example we will set up the project using the Single-View Application template. We will add more views later also known as scenes in a storyboard file. for the sake of simplicity I will name the project Unwind. It is important to leave the storyboard option checked if you are creating your own project.

Storyboard Layout - Scene 1

Once the project is created, open the storyboard file. At present there is only one UIViewController, but I will add a second one by dragging it from the Object Library unto the canvas of the storyboard. On the first scene add a UIButton and a UILabel. Change the identifier of the button to Scene 2 and change the UILabel identifier to Scene 1. Repeat this exercise with the second view controller of the second scene.

Now control drag (ctrl+drag) a connection from the button of the first scene to the view of the second scene. The connection between to the two scenes is called a segue. When you release the mouse button, a popover will appear to allow a developer to select one of three segue actions. For this exercise I will use the modal action. The following screenshot provides a visual on the storyboard layout. Now compile and run the app and click on the button on the first scene.

iOS 6 storyboard layout and Exit object
iOS 6 storyboard layout and Exit object | Source

Storyboard Layout - Scene Integration

To send data to another scene via the segue object, all that was required was to implement the prepareForSegue method. In the body of the method you typically created a new view controller object of the target View Controller. Then you could assign the value to a field or label of the target view controller. When the target scene with UIViewController is loaded on the stack, the data from the calling scene would be in the second scene. This worked great from one scene to another but this architecture was a one-way only. To push data from the target scene back to the calling scene, you needed to use global variables or implement a protocol. You could create a segue from Scene 2 to Scene 1 but this would create new instances of the ViewController, thus using up all the available for the application.

prepareForSegue Implementation

///Import view controller of Scene 2 after you create it and assign it to the second view controller.
#import <UIKit/UIKit.h>
#import "Scene2ViewController.h"

@interface ViewController : UIViewController

@end

///Implementation file of Scene 1 
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    Scene2ViewController *scene2 = [segue destinationViewController];
    //push data like to the UILabel after you have created an IBOutlet
    
}

With iOS 6, Apple has engineered a new object called the Exit. It is located at the bottom of each UIViewController or any other view controller for that matter.The screenshot above has the Exit object highlighted.

Storyboard Layout - Scene Integration

To send data to another scene via the segue object, all that was required was to implement the prepareForSegue method. In the body of the method you typically created a new view controller object of the target View Controller. Then you could assign the value to a field or label of the target view controller. When the target scene with UIViewController is loaded on the stack, the data from the calling scene would be in the second scene. This worked great from one scene to another but this architecture was a one-way only. To push data from the target scene back to the calling scene, you needed to use global variables or implement a protocol. You could create a segue from Scene 2 to Scene 1 but this would create new instances of the ViewController, thus using up all the available for the application.To implement the prepareForSegue as in the code listing above, you would need to create this view controller first and add an import statement to the first view controller.

You could also create IBoutlets for the UILabels so that when the scene is called the text in the label would change also.

With iOS 6, Apple has engineered a new object called the Exit. It is located at the bottom of each UIViewController or any other view controller for that matter.The screenshot above has the Exit object highlighted.

To implement the Unwind object, start by creating a second view controller custom class as a subclass of the UIViewController. I called this second view controller Scene2ViewController. Next I selected the second view controller in the storyboard and added the Scene2ViewController to the custom class field. Next add an import statement of the first view controller to the header file like in the following code listing.

Then we will need to create a method to be called by the Unwind object. In the header file of the Scene1 view controller add a method, called backtoScene1 with an IBAction as a return type and a UIStoryboardSegue as an argument as in the code below. To maker this example a bit more interesting, I will create an IBOutlet for each of the UILabels in the Stoyboard. To do this, select each UILabel in turn and open the header file using the Editor assistant. Then drag (ctrl+drag) a connection from the UILabel to the open header file. In the resulting popover, name each IBOutlet Scene1Lbl and Scene2Lbl respectfully. Now we can call these objects from the view controller classes.

Open the implementation file for the ViewController (Scene1) and add the newly created method as in the code below. In the body of the method set the value of the IBOutlet scene1Lbl to “Back from Scene 2”.

All that is need is to Ctrl+drag a connection from the Scene2 button to the Exit (Unwind) object in the Scene2 View Controller. In the popover you should see the method that was created in the Scene1 view controller (after adding the import statement). Select the backtoScene1 method then compile and run the app. Click on the Scene1 button to move to the Scene2, then click on the Scene2 button to return to scene 1. That’s it.

ViewController.h

//
//  ViewController.h
//  Unwind
//
//  Created by Kevin Languedoc on 12/30/12.
//  Copyright (c) 2012 Kevin Languedoc. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Scene2ViewController.h"

@interface ViewController : UIViewController

-(IBAction)backToScene1:(UIStoryboardSegue*)segue;

@property (strong, nonatomic) IBOutlet UILabel *scene1Lbl;
@end

ViewController.m Implementation

//Code snippet only of backToScene1 implementation
...

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    Scene2ViewController *scene2 = [segue destinationViewController];
    scene2.scene2Lbl.text = @"Here from scene 1";
    
}

-(IBAction)backToScene1:(UIStoryboardSegue*)segue{
    scene1Lbl.text = @"Back from scene 2";
}
Implementing the Unwind (Exit) object
Implementing the Unwind (Exit) object | Source

More by this Author


Comments 2 comments

Victoria Lynn profile image

Victoria Lynn 3 years ago from Arkansas, USA

Wow, Kevin, I am impressed by your hubs, even though I don't understand them. LOL. This hub is well laid out with the photos to help. I'm sure this niche is well read online, and I hope you're getting lots of traffic to these. Your profile and all the hubs on this topic is very impressive.


klanguedoc profile image

klanguedoc 3 years ago from Canada Author

Thanks Victoria Lynn. Yes I do get a lot of reads as it is a popular niche, but I also enjoy the subjects I write about and I try to write tutorials that I would want to read. I appreciate the feedback :)

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working