Storyboard
iOS development
modal segue
push segue
app design

What is the difference between Modal and Push segue in Storyboards?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In iOS development with Storyboards, understanding the difference between Modal and Push segues is crucial for delivering a seamless user experience. Both are navigation techniques used to transition between view controllers, but they serve different purposes and involve distinct behaviors and configurations.

Understanding Segues

Before diving into the specifics of Modal and Push segues, let's briefly explore what a segue is. A segue in iOS represents a transition between two view controllers in a storyboard. Segues can be triggered by user interactions, such as tapping a button, or programmatically in the code.

Push Segue

A Push segue is commonly used within a UINavigationController and involves a hierarchical navigation structure. It adds a new view controller to the navigation stack, allowing the user to go back to the previous view controller.

Key Characteristics:

  • Navigation Hierarchy: Used to move down the hierarchy in a stack-based navigation pathway.
  • Back Navigation: A back button is automatically provided at the top of the new view, allowing the user to pop back to the previous view controller.
  • Navigation Bar: The new view controller is presented with a navigation bar at the top, which is consistent with the navigation stack.

Example Usage:

objc
1- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
2    if ([segue.identifier isEqualToString:@"showDetail"]) {
3        DetailViewController *detailVC = segue.destinationViewController;
4        detailVC.data = self.selectedData;
5    }
6}

A Modal segue presents a new view controller modally. This means the new view controller is presented over the existing one and is typically used for tasks that require the user's attention, such as input forms or actions.

Key Characteristics:

  • Standalone Presentation: Does not use a navigation stack and is not part of any existing hierarchical navigation.
  • Dismissal: The presented view must handle its own dismissal, often with a "Cancel" or "Done" button.
  • Full-Screen/Partial-Screen View: Can be configured to appear full screen, as a page sheet, or in other presentation styles as needed.
  • Overlay: The existing view controller is not removed from view hierarchy, but is overlaid by the newly presented view.

Example Usage:

objc
1- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
2    if ([segue.identifier isEqualToString:@"presentDetail"]) {
3        DetailViewController *detailVC = segue.destinationViewController;
4        detailVC.modalPresentationStyle = UIModalPresentationFullScreen;
5    }
6}

Key Comparisons

AspectPush SegueModal Segue
Usage ContextNavigation within a hierarchyIndependent task or need for overlay
Navigation ControlBack button provided automaticallyRequires manual dismissal
View Controller ScopePart of navigation stackPresented on top of existing stack
Navigation BarAppears due to navigation controllerTypically no navigation bar, but customizable
DismissalPopped off the stack via navigationRequires explicit dismissal action

Additional Considerations

Transition Animations

Both Modal and Push segues support custom animations. With a Push segue, the navigation controller typically handles the transition animation as a slide from right to left. For Modal segues, you can define custom transition styles and animations using UIViewControllerAnimatedTransitioning.

Presentation Styles

When using a Modal segue, you can customize the presentation style. Options include full screen, page sheet, or form sheet. The choice of style can affect the user experience and the appearance of underlying view controllers.

Dynamic Data Passing

Both segue types allow for passing data to the destination view controller. This is often done within the prepareForSegue:sender: method, where you cast the destination view controller, then set properties based on the current context.

Best Practices

  • Use Push segues for tasks involving navigating deeper into a hierarchical structure.
  • Opt for Modal segues when the task is brief or requires a different focus that doesn't fit strictly into the stack-based navigation model.

Understanding when and how to use Modal versus Push segues helps refine the user navigation experience and enables developers to leverage the full potential of Storyboards while aligning with the expectations typical to iOS applications.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.