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

117,341

Solution 1

A push Segue is adding another VC to the navigation stack. This assumes that VC that originates the push is part of the same navigation controller that the VC that is being added to the stack belongs to. Memory management is not an issue with navigation controllers and a deep stack. As long as you are taking care of objects you might be passing from one VC to another, the runtime will take care of the navigation stack. See the image for a visual indication: NavStack

A modal Segue is just one VC presenting another VC modally. The VCs don't have to be part of a navigation controller and the VC being presented modally is generally considered to be a "child" of the presenting (parent) VC. The modally presented VC is usually sans any navigation bars or tab bars. The presenting VC is also responsible for dismissing the modal VC it created and presented.

Hope this helps.

Solution 2

Swift 3.0 and XCode 8.2.1 update

1. Push Segue

Push segue has been renamed as Show segue. To create push segue, the parent view controller needs to be embedded in navigation controller. The navigation controller provides navigation bar. Once you connect two view controller with push segue, the child view controller will automatically has navigation bar on top. The child view controller will be added on top of the navigation stack.

enter image description here

Push segue also provides default features. The child view controller will have a back button that gets you back to the parent view controller. You can also swipe right to pop the child view controller. The animation for push segue is like sliding pages horizontally.

enter image description here

While you are allowed to make a push segue from a view controller that is not in a navigation controller, you will lose all the features like navigation bar, animation, gesture etc when you do so. In this case, you should embed your parent view controller inside navigation view controller first and then make push segue to child view controllers.

enter image description here

2. Modal Segue

A modal segue (i.e. present modally), on the other hand, is presenting over the current view controller. The child view controller will not inherit navigation view controller so the navigation bar will be lost if you present modal segue from a view controller with navigation view controller. You have to embed the child view controller in navigation controller again and start a brand new navigation stack if you want it back. If you want to get back to parent view controller, you have to implement this by yourself and call dismiss from code.

enter image description here

Animation for modal segue is that the child view controller will comes up from the bottom of the page. The navigation view controller is also gone in this demo

enter image description here

Solution 3

The push view must be built in a navigationController.

Click on your master view, then in the menu bar choose:

EDITOR->embed in->navigationController

Solution 4

This is pushing controls using custom push and segue methods for storyboard Story Board

And Modal is way to navigate through views without using Storyboards.

Share:
117,341

Related videos on Youtube

Gaurav_soni
Author by

Gaurav_soni

Frontend Developer , (Angularjs,Nodejs,HTML ,Less) Working at AllCAD , Jaipur My facebook ID - https://www.facebook.com/gauravsoni12345

Updated on July 08, 2022

Comments

  • Gaurav_soni
    Gaurav_soni almost 2 years

    Can someone explain to me what is the exact difference between modal and push segue?

    I know that when we use push the segue gets added to a stack, so when we keep using push it keeps occupying memory?

    Can someone please show me how these two are implemented?

    Modal segues can be created by simply ctrl-click and dragging to destination but when I do that with the push my app crashes.

    I am pushing from a button to a UINavigationController that has a UIViewController.

  • Gaurav_soni
    Gaurav_soni over 12 years
    @EIJay thanks for the great explanation. One more question when do i have to use the modal and when to use the push segue ?
  • LJ Wilson
    LJ Wilson over 12 years
    Generally, when you want to show a detail view of a summary view, use a navigation controller and Push Segues. If the "parent" view doesn't really relate as far as data is concerned to the "child" view, then use a modal. A good example for a modal view would be a Login view. The Login view doesn't really have any relationship as far as data is concerned to the "parent" view.
  • T.J.
    T.J. over 12 years
    Can a modal view controller call another modal view controller?
  • Constantino Tsarouhas
    Constantino Tsarouhas almost 12 years
    @T.J. Yes, you can create "model chains". Dismissing a VC down the chain dismisses all VCs up the chain — the user only sees the top VC dismiss (when you enable animation). It's like navigation controllers.
  • user4951
    user4951 almost 12 years
    I do not think the relationship is parent and child. It's presenter and presentee.
  • jchatard
    jchatard over 11 years
    Is there a way to have a modal view controller having a navigation bar?
  • HpTerm
    HpTerm over 11 years
    @jchatard Yes. As long as your segue is modal, in storyboard you will see that the navigation bar is not inherited. So simply click on the first VC of the modal chain you have and then click menu Editor -> Embed In -> Navigation Controller. This will set you a navigation controller that will be common to all your modal chain. Not that at that moment only the segue for displaying the navigation controller is "modal", whereas all the segues inside of you modal chain must be "push" segues.
  • Drux
    Drux over 11 years
    If it's a modal segue, does the modally presented VC have a means to learn about its presenter in its viewDidLoad method? I have a case where the presented VC has a navigation bar. The presenting VC belongs to a tab bar. In the presented VC's viewDidLoad method self.presentingViewController points to the tab bar controller (which is surprising to me) and I can't find/access the presenting VC as one of its properties.
  • jianpx
    jianpx almost 11 years
    How to create modal segue without using storyboards?
  • Pushkraj Lanjekar
    Pushkraj Lanjekar almost 11 years
    @jianpx : You can segue only when you use storyboards. Otherwise you can use Navigation controller or PresentModalViewController for switching between views.
  • jianpx
    jianpx almost 11 years
    @ Pushkraj thanks. So do you mean there is no way to create segue by code?
  • Pushkraj Lanjekar
    Pushkraj Lanjekar almost 11 years
    @jianpx : Not needed. Segue only needed when using storyboards.
  • SmileBot
    SmileBot almost 11 years
    Push can only be used IF the VC you're segueing from is using a navigation controller. Try to push from a VC that is not embedded in a navigation controller and it will throw an exception. So, this has really nothing to do with how tightly the data is connected between parent and child, it has to do simply with whether you're using a navigation controller to manage your VC's. You can do exactly the same thing manually using segues using modal transitions.
  • LJ Wilson
    LJ Wilson almost 11 years
    That is a given, but the decision of whether to use a navigation stack is influenced by the type and hierarchy of data you want to present. Read my comment on Feb 22, 2012.
  • Crashalot
    Crashalot almost 9 years
    @RandyMarsh what do you mean dismissing a VC down the chain? Let's assume A -> B -> C all use modal segues. In our tests, dismissing C doesn't dismiss B as well; it only dismisses C. Further, how can you distinguish between dismissing all the way to the root (i.e., A) vs. only dismissing the top VC (i.e., dismiss back to B, not A)?
  • Daksh Gargas
    Daksh Gargas over 5 years
    Wrong, pushView can also be built WITHOUT navigationController and this answer is incomplete!