The Life Cycle of a Segue
To understand how custom segues work, you need to understand the life cycle of a segue object. Segue objectsare instances ofUIStoryboardSegueor one of its subclasses. Your app never creates segue objects directly;they are always created on your behalf by iOS when a segue is triggered. Here’s what happens:
-
The destination controller is created and initialized.
-
The segue object is created and itsinitWithIdentifier:source:destination:method is called.The identifier is the unique string you provided for the segue in Interface Builder, and the two otherparameters represent the two controller objects in the transition.
-
The source view controller’s prepareForSegue:sender:method is called. See “Configuring theDestination Controller When a Segue is Triggered”(page 101).
-
The segue object’s performmethod is called. This method performs a transition to bring the destinationview controller on-screen.
-
The reference to the segue object is released, causing it to be deallocated.
Implementing a Custom Segue
To implement a custom segue, you subclassUIStoryboardSegueand implement the two methods describedearlier:
- If you override the initWithIdentifier:source:destination:method, call the superclass’simplementation, then initialize your subclass.
- Your performmethod must make whatever view controller calls are necessary to perform the transitionyou want. Typically, you use any of the standard ways to display a new view controller, but you canembellish this design with animations and other effects.
<span style="font-size:18px;">- (void)perform
{
// Add your own animation code here.
[[self sourceViewController] presentModalViewController:[self
destinationViewController] animated:NO];
}</span>