37

Please tell the difference between presentViewController and UiNavigationController? Could I use presentViewController instead of UINavigationController to navigate different views in the app? Whats the scenario to use either?

Jeff
  • 13,943
  • 11
  • 55
  • 103
user1940888
  • 475
  • 1
  • 6
  • 11
  • please think about rating some of these answers if they were of any help to you thanks. – mgr Jan 09 '13 at 10:39

4 Answers4

76

presentViewController offers a mechanism to display a so-called modal view controller; i.e., a view controller that will take full control of your UI by being superimposed on top of a presenting controller.

UINavigationController offers a much more flexible mechanism where you can push a new controller, and later pop it, so to go back to the previous one, in a ordered way. Imagine that controllers in a navigation controller will just build a sequence from left to right.

I think that presentViewController is most suitable for use with just one view controller being presented at a time. You can surely use it to stack more view controllers one on top of the other (and thus sort of "mimic" a poor-man's navigation controller), but my bet is you will quickly find something not working as you expected.

Specifically, an example of such limitation is the following: when you dismiss a modal view controller (in order to "close" it), all of your modally presented view controllers (from the same presenting controller) will also be dismissed at once. So you simply will not be able to implement a "go back"/navigation like functionality.

So, it depends on what you are trying to do.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • Excellent! Thanks for the concise and accurate answer! – user1940888 Jan 09 '13 at 10:36
  • 2
    glad to have helped but don´t forget to accept the answer if you found it useful! – sergio Jan 16 '13 at 19:52
  • @sergio: I am learning iOS. I have the same question. As I know, presentViewController/dismiss also maintain a navigation stack too. Both UINagivationController & presentViewController-dismiss method Only support 1 view controller visible ( presenting) at a time. So It seems that your answer is not very clear. Sorry If this wondering is wrong. – LHA Sep 07 '15 at 22:51
  • Thanks, I reworded my answer to make it clearer, I hope, when explaining the thing about using just 1 view. – sergio Sep 08 '15 at 10:08
27

A UINavigationController is a subclass of UIViewController that manages a stack of view controllers and adds a back button etc.

presentViewController is a method of the UIViewController class you use to present a modal view controller.

Andrew Tetlaw
  • 2,669
  • 22
  • 27
  • You could hide the navigation bar, in which case the navigation controller performs the same role as a modal presentation, doesn't it? Both maintain a stack -- a modally presented controller can present another controller modally, resulting in a stack. So, it seems that the difference between modal presentation and a navigation controller aren't as clear as your answer says. – Kartick Vaddadi Feb 28 '17 at 10:41
5

The UINavigationController maintains a navigation stack for you. You are then able to navigate through hierarchical content.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

If you use UIViewControllers presentViewController method you are basically just replacing the view controller. no navigation stack is maintained for you.

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
mgr
  • 342
  • 2
  • 7
  • 6
    Are you sure No navigation stack if we use presentViewController. If you call "dismiss__", You will navigate to Previous viewController instance. It means both method will maintain navigation stack. – LHA Sep 07 '15 at 22:54
  • 1
    presentViewController does NOT replace the previous view controller. The older controller will be there when you dismiss the presented view controller. Besides, as Loc said, a modally presented controller can in turn present another modally, producing a stack. – Kartick Vaddadi Feb 28 '17 at 10:43
3

UINavigationController is a class, presentViewController is an instance method of UIViewController (iOS 5 + ), of which UINavigationController is a subclass.

pushViewController is a comparable method to presentViewController. It is an instance method of UINavigationController, for iOS 2 +

Andy
  • 14,260
  • 4
  • 43
  • 56