How to segue programmatically in iOS using Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In iOS development, a segue is a transition between two view controllers in your app's storyboard. While this is often done visually using Interface Builder, you might need to trigger segues programmatically under certain conditions. This could be based on user actions or data-driven logic within your app. In this article, we explore how to perform these programmatically using Swift.
Setting up Segues in Storyboard
First, you need to define a segue in your storyboard. This involves creating a connection between the source and destination view controllers. Follow these steps:
- Open your storyboard: Navigate to your desired view controller that will initiate the segue.
- Control-drag between view controllers: Hold the
Controlkey and drag from the source view controller to the destination view controller. - Select segue type: Choose the type of segue (e.g., Show, Present Modally, etc.).
- Assign an identifier: Select the segue and assign it an "Identifier" in the Attributes Inspector. This identifier is critical for programmatically initiating the segue.
Once you've defined the segue in storyboard, it's time to segue programmatically in your code.
Using performSegue Method
The primary method to segue programmatically is performSegue(withIdentifier:sender:). Below is a technical explanation and example of how you can use this method in a Swift project.
Example of Programmatically Triggering a Segue
Suppose you have a login screen that segues to a dashboard screen upon successful authentication. You can trigger this segue after verifying login credentials.
Breaking Down the Code
- IBOutlet properties: These connect your text fields from the storyboard to your code.
@IBActionfor Login Button: Tied to your login button's "Touch Up Inside" event.- Authentication Logic: Simple check for demonstration. Replace with real authentication process.
- Triggering Segue:
performSegue(withIdentifier:sender:)uses the identifier assigned in the storyboard. - Displaying Alert: Presents an
UIAlertControllerto show login errors.
Preparing for Segue
Before transitioning, you can pass data to the destination view controller by overriding the prepare(for:sender:) method. Here's an example:
Key Considerations
- Identifier Check: Ensure you check the segue identifier to execute the correct logic.
- Passing Data: Set properties on the destination view controller (e.g., user details).
Table: Comparison of Segue Types
| Segue Type | Description | Use Case |
| Show | Pushes a view controller on the navigation stack. | Navigating deeper in a flow within a navigation controller. |
| Present Modally | Presents a view controller modally with various presentation styles like full screen or page sheet. | Transitions that interrupt the current flow, like a settings or profile screen. |
| Custom | Provides custom animations or transitions. | Unique transitions that don't fit standard patterns. |
| Unwind | Returns to a previously visited view controller, removing intermediate view controllers from the navigation stack. | Useful for going back to initial or previous steps without re-entering every layer. |
Conclusion
Programmatically triggering segues in an iOS app using Swift provides flexibility and control over the navigation flow, especially for condition-based transitions. With the performSegue method and prepare(for:sender:), you can efficiently manage view controller transitions while passing data between them. Ensure that you clearly define your segues in the storyboard and use identifiers wisely for smooth, bug-free navigation.
Related reading
- How to segue programmatically in iOS using Swift
- How to select an item in a ListView programmatically?
- How to select Multiple images from UIImagePickerController
- How to send a POST request with BODY in swift
- How to send an object from one Android Activity to another using Intents?
- How to send an object from one Android Activity to another using Intents?
- How to send emails from my Android application?
- How to send parameters from a notification-click to an activity?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.