ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Computer Programming Tutorials

iOS 6 Storyboard Unwind Scene

Updated on December 31, 2012
klanguedoc profile image

Kevin is Software Developer with 20 years experience designing and building software applications including iOS and Android apps.

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

Comments

    0 of 8192 characters used
    Post Comment

    • klanguedoc profile image
      Author

      Kevin Languedoc 5 years ago from Canada

      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 :)

    • Victoria Lynn profile image

      Victoria Lynn 5 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.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: "https://hubpages.com/privacy-policy#gdpr"

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)