Differencebetween Modal and Push Segue in Storyboards

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

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.

Difference between push and modal segues

Push Segue

It adds a new VC to navigationController. if we have requirement to manage our views in heiraricall manner we should use it. it adds new view to navigation stack . Back button is displayed . on clicking back button it popped the VC from navigationController.

Modal Segue

A modal segue is just one VC presenting another VC modally. No navigation stack created.Don't display back button . You have to create as when needed cusotmly.

What's the difference between all the Selection Segues?

Here is a quick summary of the segues and an example for each type.

Show - Pushes the destination view controller onto the navigation stack, sliding overtop from right to left, providing a back button to return - if not embedded in a navigation controller it will be presented modally

Example: Navigating in Settings, for example tapping General > About

Show Detail - For use in a split view controller, replaces the secondary view controller when in a multi-column interface, or if collapsed to one column it will push in the navigation controller

Example: In Messages, tapping a conversation will show the conversation details - replacing the view controller on the right when in a two column layout, or push the conversation when in a single column layout

Present Modally - Presents a view controller overtop the current view controller in various fashions as defined by the modal presentation and transition style - most commonly used to present a view controller in a sheet that animates up from the bottom

Example: Selecting Face ID & Passcode in Settings

Popover Presentation - When run on iPad, the destination appears in a popover, and tapping anywhere outside will dismiss it - popovers are supported on iPhone as well but by default it will present the view controller modally

Example: Tapping the + button in Calendar

Custom - You may implement your own custom segue and have control over its behavior

Embed - You may embed a view controller into another view controller, such as navigation, tab bar, and split view controllers as well as custom containers

Unwind - You may use an unwind segue to navigate back to a previous view controller, even if there’s many screens pushed/presented on top all of them will be dismissed

The deprecated segues are essentially the non-adaptive equivalents of those described above. These segue types were deprecated in iOS 8: Push, Modal, Popover, Replace.

For more info, you may read over the Using Segues documentation which also explains the types of segues and how to use them in a Storyboard. Also check out Session 216 Building Adaptive Apps with UIKit from WWDC 2014. They talked about how you can build adaptive apps using these new Adaptive Segues, and they built a demo project that utilizes these segues.

Use case for push versus modal segues?

If you want to follow Apple's best practices, I would suggest the following :

  1. For the "Add" functionality, use a modal segue.

    For example look at the contacts app. Pressing + shows a modal view controller.

    What's the logic ? for start, modal view controllers usually have a "cancel" button, as opposed to the "back" button on a pushed vc.

    When the user presses "back" - he'd expect a way to come back to the vc. Usually "back" saves your data on iOS (auto-saved).

    So by using a modal segue you force the user to submit the form , or cancel. The modal presentation hints that you really need to fill this screen.

  2. For editing - push. but modal could work as well (and you could reuse the same VC).

    Reasons for push :

    • you get a hierarchy of vc's , going back and forward while drilling down.
    • (you should implement) auto saving when going back (just like other iOS apps)

how to know if segue was push or modal

If your view controller has its property presentingViewController set to something other than nil, then it's being presented modally by that controller. Else, you can assume it's been pushed (thus, self.parentViewController should be an UINavigationController).

iOS storyboard: modal and push segue on the same viewcontroller

The default navigation bar is comes with a navigation controller when you push a view controller to the navigation controller. When you connect

  1. A0-B, using a push segue, navigation bar will automatically come since it is pushed to a navigation controller.
  2. A1-B, using a modal segue, navigation bar disappears since it is not under a navigation controller.

So what you can do is add a navigation controller before A1 and push B. Or programmatically add a navigation bar after present it from A1. If you push from A0 bar will automatically come

Modal vs Push - which is correct for my scenario?

According to Apple's Human Interface Guidelines:

Use a modal view when you need to offer the ability to accomplish a
self-contained task related to your app’s primary function. A modal
view is especially appropriate for a multistep subtask that requires
UI elements that don’t belong in the main app UI all the time.

Modal Views

This is largely a preference thing and depends upon what you are trying to accomplish and whether you care if the user explicitly acknowledges his edits or not (i.e. save/cancel). It sounds like you have a reasonable case for push in your example so I would go with that. If you find that you are moving toward implementing features of a modal VC while using a push, then switch over to modal. I do not believe there is a hard and fast rule for this.

The following answer provides some additional nice reasons to go modal or push

Modal vs. Push

iOS Storyboard Presenting Segues relationship, embed, push, modal, custom types

A "relationship" segue is the segue between a container view controller and its child or children -- so, the initial controller of a navigation controller, the view controllers in the tabs of a tab bar controller, and the master and detail controllers of a split view controller.

An "embed" segue is the segue between a container view and the controller that's embedded in that container view that you get automatically when you add a container view to a controller's view.

Both of these segues are executed as soon as the parent controller gets instantiated. You do not call them, but you can implement prepareForSegue, and pass information to the destination view controller.

View appears with modal view animation instead of show (push) animation

After taking a look at the Storyboard in the project, I discovered that the problem was due to extra navigation controllers after each Push segue. That is, in addition to the initial (root) navigation controller, the Storyboard contained a UINavigationController as the destination for the problematic Push segues. Removing these extra navigation controllers (but keeping the root navigation controller) solved the problem.



Related Topics



Leave a reply



Submit