Wednesday, January 18, 2012

Presenting Storyboard View Controllers Programmatically

Although the storyboard runtime usually handles transitions between view controllers, you can also trigger segues programmatically from your code. You might do so when setting up the segue in Interface Builder is not possible, such as when using accelerometer events to trigger the transition. There are several options for transitioning to a new view controller:

  • If a storyboard file contains an existing segue between the current view controller and the destination view controller (perhaps triggered by some other control in the view controller), you can trigger that segue programmatically using the performSegueWithIdentifier:sender:method of UIViewController.
  • If there is no segue between the view controllers but the destination view controller is defined in the storyboard file, first load the view controller programmatically using the instantiateViewControllerWithIdentifier: method of UIStoryboard. Then present the view controller using any of the existing programmatic means, such as by pushing it on a navigation stack.
  • If the destination view controller is not in the storyboard file, create it programmatically and present it as described in View Controller Programming Guide for iOS.