An object that prepares for and performs the visual transition between two view controllers.
- iOS 5.0+
- tvOS 9.0+
UIStoryboard class supports the standard visual transitions available in UIKit. You can also subclass to define custom transitions between the view controllers in your storyboard file.
Segue objects contain information about the view controllers involved in a transition. When a segue is triggered, but before the visual transition occurs, the storyboard runtime calls the current view controller’s
prepare(for: method so that it can pass any needed data to the view controller that is about to be displayed.
You do not create segue objects directly. Instead, the storyboard runtime creates them when it must perform a segue between two view controllers. You can still initiate a segue programmatically using the
perform method of
UIView if you want. You might do so to initiate a segue from a source that was added programmatically and therefore not available in Interface Builder.
You can subclass
UIStoryboard in situations where you want to provide a custom transition between view controllers in your application. To use your custom segue, create a segue line between the appropriate view controllers in Interface Builder and set its type to Custom in the inspector; you must also specify the class name of the segue to use in the inspector.
When the storyboard runtime detects a custom segue, it creates a new instance of your class, configures it with the view controller objects, asks the view controller source to prepare for the segue, and then performs the segue.
Methods to Override
For custom segues, the main method you need to override is the
perform() method. The storyboard runtime calls this method when it is time to perform the visual transition from the view controller in
source to the view controller in
destination. If you need to initialize any variables in your custom segue subclass, you can also override the
init(identifier: method and initialize them in your custom implementation.
Alternatives to Subclassing
If your segue does not need to store additional information or provide anything other than a
perform() method, consider using the
init(identifier: method instead.