In this recipe, we will learn how we can use the storyboard features to programmatically present view controllers within the storyboard.
In this recipe, we will look at how we can programmatically call another view controller within the storyboard by using its identifier.
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:
TwitterExample
folder from the project navigator.SecondViewController
as the name of the class to create.Once you have done this, we need to
update the class method of our second view controller to use our SecondViewController
subclass:
SecondViewController.h
interface file, located under the TwitterExample
folder, from within the project navigator.// 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
SecondViewController.m
implementation file, located under the TwitterExample
folder, from within the project navigator.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]; } }
SecondViewController
.irstViewController
as the unique identifier to use.SecondViewController
.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.
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.
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"];
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.