<!--
{
  "documentType" : "article",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/customizing-the-behavior-of-segue-based-presentations",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Customizing the behavior of segue-based presentations"
}
-->

# Customizing the behavior of segue-based presentations

Pass data between view controllers during a segue, and programmatically control when segues occur.

## Discussion

When the user triggers a segue, UIKit presents the view controller using the options found in your storyboard. UIKit also provides opportunities for you to modify the segue process dynamically, either by preventing the segue from occurring or by doing extra work when a segue occurs.

For information on how to create a segue, see [Specify presentations visually in your storyboard file](/documentation/UIKit/showing-and-hiding-view-controllers#Specify-presentations-visually-in-your-storyboard-file).

### Configure the presentation style of the transition

The segue’s type determines what kind of animations UIKit uses when presenting and dismissing the segue, as the table below shows. You specify the type when you create the segue, but you can also change it in the attributes inspector later.

|Segue type           |Behavior                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
|---------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|Show (Push)          |Displays the view controller modally unless a parent view controller implements the ``doc://com.apple.uikit/documentation/UIKit/UIViewController/show(_:sender:)`` action method, in which case that view controller defines the presentation behavior. For example, a ``doc://com.apple.uikit/documentation/UIKit/UINavigationController`` pushes the new view controller onto its navigation stack.                                                         |
|Show Detail (Replace)|Displays the view controller modally unless a parent view controller implements the ``doc://com.apple.uikit/documentation/UIKit/UIViewController/showDetailViewController(_:sender:)`` action method, in which case that view controller defines the presentation behavior. For example, a ``doc://com.apple.uikit/documentation/UIKit/UISplitViewController`` replaces its second child view controller (the detail controller) with the new view controller.|
|Present Modally      |Displays the view controller modally using the specified presentation and transition styles.                                                                                                                                                                                                                                                                                                                                                                  |
|Present as Popover   |In a horizontally regular environment, UIKit presents the view controller in a popover. In a horizontally compact environment, UIKit presents the view controller modally.                                                                                                                                                                                                                                                                                    |

For more information about how UIKit performs segues involving the Show and Show Detail presentation styles, see [Let the current context define the presentation technique](/documentation/UIKit/showing-and-hiding-view-controllers#Let-the-current-context-define-the-presentation-technique).

### Prevent a segue based on dynamic conditions

When you don’t want the user to leave the current view controller, tell UIKit not to perform a segue by returning false from the [`shouldPerformSegue(withIdentifier:sender:)`](/documentation/UIKit/UIViewController/shouldPerformSegue(withIdentifier:sender:)) method of the source view controller. Use that method to perform any checks you need to determine whether the segue can proceed. For example, return <doc://com.apple.documentation/documentation/Swift/false> if the view controller’s content is invalid and requires corrective user actions. Returning true lets the segue continue, but returning <doc://com.apple.documentation/documentation/Swift/false> causes the segue to fail silently.

### Pass data to the presented view controller

Because UIKit creates and presents the view controller automatically during a segue, use the [`prepare(for:sender:)`](/documentation/UIKit/UIViewController/prepare(for:sender:)) method to pass any data to that view controller before the segue occurs. Implement the method on the view controller containing the object that initiated the segue. Fetch the new view controller from the provided [`UIStoryboardSegue`](/documentation/UIKit/UIStoryboardSegue) object, along with information about which segue was triggered.

In the following code example, the current view controller fetches the image associated with the selected table row and passes that image to the new view controller.

```swift
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  // Get the new view controller.
   if let imageVC = segue.destination as? ImageViewController {

      // Fetch the image for the selected row. 
      let image = getImageForSelectedRow()
      imageVC.currentImage = image
   }
}
```

Use the delegate design pattern to pass data from a presented view controller back to the originating view controller. Configure that delegate relationship in your [`prepare(for:sender:)`](/documentation/UIKit/UIViewController/prepare(for:sender:)) method.

### Understand the sequence of events during a segue

Although UIKit handles segues automatically, there are many places where you can perform work related to displaying the new view controller. The following figure shows the flow of events that happens from the time the user triggers a segue until the process is complete. The main place to perform segue-related actions is the current view controller’s [`prepare(for:sender:)`](/documentation/UIKit/UIViewController/prepare(for:sender:)) method, but you also may perform tasks during the creation of the new view controller.

![An illustration of the flow of events that occur when the user triggers a segue. UIKit calls methods of the current view controller to modify the segue’s behavior. ](images/com.apple.uikit/media-3379644@2x.png)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)