iOS - pushViewController vs presentModalViewController difference

前端 未结 5 827
南旧
南旧 2021-02-01 17:17

What is the difference beetween calling presentModalViewController and pushViewController, when :

  • animation is set to NO (even if yes, th
相关标签:
5条回答
  • 2021-02-01 17:51

    This is what my experience says,if you want to manage a hierarchy of views,better go for pushViewController in the navigation controller. It works like a stack of view-controllers in the navigation controller. If however the requirement is just to show a view on executing some actions on the parent view controller then the best way is presenting it modally. If you need a complex push pop logic always prefer a pushViewController.

    0 讨论(0)
  • 2021-02-01 17:55

    The most important difference is about semantics. Modal view controllers typically indicate that the user has to provide some information or do something. This link explains it more in depth: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

    Here's another, less abstract difference they talk about:

    "When you present a modal view controller, the system creates a parent-child relationship between the view controller that did the presenting and the view controller that was presented. Specifically, the view controller that did the presenting updates its modalViewController property to point to its presented (child) view controller. Similarly, the presented view controller updates its parentViewController property to point back to the view controller that presented it."

    Also see this thread: why "present modal view controller"?

    0 讨论(0)
  • 2021-02-01 18:03

    Take a look into the viewControllers in the image

    • The top 2 viewControllers(login & submit) at the top left are disconnected from the tabBarController & NavigationController
    • The rest of the viewControllers are embedded in a NavigationController. They somehow belong to the natural flow of the app.

    Now you have to ask yourself

    Do I need to always show login + submit page every time? It would be pain in the neck for the user to each time go to login even if they logged in last time. These 2 screen really don't fit the natural flow of the screens. So what do we do? We just add them modally using presentViewController

    However for the rest of the viewControllers we want to keep them inside 2 navigation so we can easily go back and forth so we use pushViewController

    For more information I recommend you to see this video The image was also picked from this great answer. It's worthy of a look.

    0 讨论(0)
  • 2021-02-01 18:12

    Ignoring transitions/animations and how things are structured behind the scenes (which aleph_null's alswer provides a good discussion of), the only user-facing difference is the ability to return to the preceding view automatically using the navigation bar.

    If you use pushViewController you will automatically get a "Back" button in the navigation bar. If you use presentModalViewController you do not, and generally will have to implement your own controls and/or callbacks to handle dismissing the controller.

    Conceptually the modal presentation style is generally used for atomic tasks that you cannot navigate away from (i.e. you either complete the task, or you cancel, and you cannot do anything else within the app until you do one or the other).

    If you're wondering why have the difference in the first place, I can't say. Personally I think frameworks that provide a unified API for moving from one controller to another (like cocos2d, or Android) make a lot more sense.

    0 讨论(0)
  • 2021-02-01 18:16

    UINavigationController are used when you want to have some sort of hierarchal representation of your data (ie drill down). They work using a stack of UIViewController subclasses. Every time you “drill down”, you simply add another view controller to the stack. Then, the “back” logic is simply a matter of popping view controllers off of a stack.

    You can check out this link: http://www.icodeblog.com/2011/10/11/back-to-basics-an-introduction-to-view-controllers/

    0 讨论(0)
提交回复
热议问题