iOS - pushViewController vs presentModalViewController difference

Oliver picture Oliver · Nov 8, 2011 · Viewed 20k times · Source

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

  • animation is set to NO (even if yes, that's just an animation style that can be changed).
  • a navigation controller is defined when presenting the modal view, so it can be navigable too, with a call stack, ....

Is this just to be able to go back from the first pushed view ? Woooaaaaaa.....

I guess the difference is elsewhere and deeper. No ?

Answer

aroth picture aroth · Nov 8, 2011

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.