tezvyn:

Storyboard Segues: Visual UI Flow in iOS

AI-drafted, machine-checkedSource: developer.apple.combeginner
Storyboard Segues: Visual UI Flow in iOS

A Storyboard Segue is a visual arrow connecting two screens in your iOS app, defining a navigation path without writing code. Use them to transition from a list to a detail view. The biggest mistake is forgetting to use `prepare(for:sender:)` to pass data.

WHY IT EXISTS Storyboard Segues were created to let developers define an application's flow and navigation visually, reducing the amount of boilerplate code needed for simple screen transitions. This makes the app's structure easier to understand at a glance by looking at the storyboard file.

THE MENTAL MODEL A segue is a directed arrow on your app's map. It starts at a specific user action on one screen, like a button tap, and points to the destination screen that should appear as a result. It encapsulates the entire transition logic into a single visual object.

HOW IT WORKS In Xcode's Interface Builder, you create a segue by control-dragging from a UI element (like a button or table view cell) to another view controller. This creates a segue object in the storyboard. When the user triggers the action, UIKit automatically instantiates the destination view controller and performs the transition. To pass data, you implement the prepare(for:sender:) method in the source view controller. This method is called just before the transition, giving you a chance to access the destination view controller instance and set its properties.

WHEN TO USE IT Segues are ideal for apps with clear, mostly linear navigation paths, especially for smaller projects or for prototyping. They excel at showing the relationship between screens, such as a master-detail flow where a list navigates to an item's detail page, or for presenting modal screens like a composer or login form.

WHEN NOT TO USE IT Avoid segues in apps with very complex, dynamic, or conditional navigation. As a project grows, a single large storyboard can become a source of merge conflicts for teams and slow to load in Xcode. For these cases, programmatic navigation (creating and presenting view controllers in code) offers more flexibility and is easier to manage and test.

ONE CANONICAL EXAMPLE A classic use is a master-detail interface. You have a UITableViewController showing a list of items. You create a segue from the table view's prototype cell to a DetailViewController. In prepare(for:sender:), you get the indexPath of the selected cell, find the corresponding data model, and pass that model object to a property on the DetailViewController instance. When the user taps a cell, the segue automatically triggers, pushing the fully configured detail screen onto the navigation stack.

Read the original → developer.apple.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.