Presenting storyboard view controllers programmatically

In this recipe, we will learn how we can use the storyboard features to programmatically present view controllers within the storyboard.

Getting ready

In this recipe, we will look at how we can programmatically call another view controller within the storyboard by using its identifier.

How to do it…

In order to programmatically determine which view we are in, we will need to create a new UIViewController subclass for our second view controller. This can be achieved by performing the following simple steps:

  1. Select the TwitterExample folder from the project navigator.
  2. Select File | New | File… or press command + N.
  3. Select the Objective-C class template from the list of templates.
  4. Enter SecondViewController as the name of the class to create.
  5. Ensure that you have selected UIViewController as the type of subclass to create from the Subclass of drop-down list.
  6. Ensure that the Targeted for iPad checkbox has not been checked.
  7. Ensure that the With XIB for User Interface checkbox has not been checked.
  8. Click on Next to proceed to the next step in the wizard.
  9. Specify the location to save the class file, and then click on the Create button.

Once you have done this, we need to update the class method of our second view controller to use our SecondViewController subclass:

  1. Open the SecondViewController.h interface file, located under the TwitterExample folder, from within the project navigator.
  2. Modify the interface file by adding the highlighted code sections, as shown in the following code snippet:
    //  SecondViewController.h
    //  TwitterExample
    //
    //  Created by Steven F Daniel on 21/09/12.
    //  Copyright (c) 2012 GenieSoft Studios. All rights reserved.
    
    #import <UIKit/UIKit.h>
    
    @interface SecondViewController : UIViewController <UIAlertViewDelegate>
    
    @end
  3. Open the SecondViewController.m implementation file, located under the TwitterExample folder, from within the project navigator.
  4. Create the prepareForSegue:sender method, as shown in the following code snippet:
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
      // Ensure we are processing the correct segue 
       // within the Storyboard
      if ([segue.identifier 
    isEqualToString:@"firstViewController"])
      {
        UIAlertView *alertView = [[UIAlertView alloc] 
    initWithTitle:@"Twitter Example"
      message:@"Currently displaying View #1"
        delegate:self
        cancelButtonTitle:@"OK"
        otherButtonTitles:nil];
    
        [alertView show];
      }
    }
  5. Select the second view controller and click on the Identity Inspector button.
  6. Next, change the Custom Class property to read SecondViewController.
    How to do it…
  7. Next, we need to apply the same storyboard segues for our first view controller.
  8. Select the Attributes Inspector section, and then under the StoryboardSegue section, enter FirstViewController as the unique identifier to use.
  9. Repeat the same steps as we did for the SecondViewController.
  10. Next, build and run the application by selecting Product | Run from the Product menu, or alternatively by pressing command + R.

When the compilation completes, the iOS Simulator will appear, showing the programmatic transitions between each of the view controllers that are defined within our storyboard.

How it works...

When you click on the About App button, it transitions over to the second view controller and then displays the message based on the prepareForSegue:(UIStoryboardSegue*)segue method call, determining the identifier of the current view controller that is being displayed within the view.

Clicking on the Go Back button will transfer control over to the first view controller; a call is made to the prepareForSegue method to determine the current identifier of the current view.

There's more…

Although the storyboard runtime usually handles transitions between view controllers, you can also trigger segues programmatically directly from within your code. You may choose to do this when setting up the segue from within the XIB editor in Xcode, or you may want to use the accelerometer events to trigger a transition and display a graphic animation.

If you take a look at the following code snippet, it shows you how you can programmatically display any view controller within your storyboard, by using the instantiateViewControllerWithIdentifier: method of the UIStoryboard class. We then use the presentViewController: method to push the view controller onto the view controller navigation stack.

SecondViewController *mvc = [self.storyboard
     instantiateViewControllerWithIdentifier:@"secondViewController"];

Note

For information on how to implement the methods of the UIStoryboard class, refer to the Apple Developer documentation, located at https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIStoryboard_Class/Reference/Reference.html.

See also

  • The Composing a tweet recipe
  • The Adding photos to a tweet recipe
  • The Applying transitions to storyboards recipe
  • The Preparing transition to another view controller recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset