Difference Between Presentviewcontroller and Uinavigationcontroller

difference between presentViewController and UINavigationController?

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.

What is the difference between UINavigationController.presentViewController and self.presentViewController?

They are analogous - both initiate a modal presentation.

In recent iOS versions, modal presentation always travels to the top most container view controller. So when your view controller (aka self) is container inside a navigation controller (aka self.navigationController), when you attempt presenting on the view controller, it will pass the presentation duties to the navigation controller. You can verify this by logging the presentingController of b once the presentation is completed. In both cases, presentingController will be the navigation controller.

navigationController!.pushViewController vs. presentViewController in iOS Swift

It appears as though when calling the presentViewController method, the view of the view controller is actually loaded during the call, whereas when calling the pushViewController on the navigation controller, the view itself is loaded after the call.

You can test this yourself by printing to the console before and after presenting/pushing the view controller, and printing to the console in the TextViewController's viewDidLoad method.

The view needs to be loaded for the textDetail variable to load (you haven't said so, but I'm assuming this is an IBOutlet) and the textDetail variable needs to load for you to be able to modify its text property.

To use pushViewController you could for example set a variable on your TextViewController class, and override its viewDidLoad method, where you could then set the text property on the textDetail variable.

UINavigationController vs viewController embed in NavigationController

UINavigationController is used where you want you move back and forth in your application. Generally Navigation controller is used when you are navigating in more detailed information in each level of depth you are in your application.
Sample Image

UIViewController is generally preferred when you display polished information. in UINavigationController generally it is the one of the last controller you push in your controller

What are limitations of presentViewController over UINavigationController

You can't push a navigation controller into other navigation controller.
You can present a navigation controller above other navigation controller.

If you push a view controller into the navigation controller, view controller's view cover only area inside navigation controller.
If you present a view controller, view controller's view cover the window hierarchy (user can't interact with other parts of the application).

You can don't use UINavigationController to present some UIViewController. You should care about "close" button to let user to close presented UIViewController

I've create test project to illustrate my answer https://github.com/K-Be/PresentTest

Difference between pushViewController and showViewController

Show segue can be used with navigation controllers, they simply push viewControllers on your stack.

Show detail segue has only sense with split view controllers. Since you have two viewControllers inside your split view controller you can:

navigate in your master view controller by presenting (pushing, since the default project uses a navigationVC as master VC) view controllers with Show segue
show details in your detail view controller with Show detail segue
In case you don't know how a Split view controller is composed:

**************++++++++++++++++++
* * +
* * +
* master * detail +
* view * view +
* controller * controller +
* * +
* * +
**************++++++++++++++++++

BUT !

On iphones it's presented like this (iPhone6+ landscape excluded)

****************
*++++++++++++++*
*+ +*
*+ +*
*+ +*
*+ detail +*
*+ view +*
*+ controller +*
*+ +*
*+ +*
*++++++++++++++*
****************

Both of Showsegue and Show detail segue are new to iOS8 & Xcode6, they are called adaptative segues, they behaves differently depending on the device type or the orientation.

Basically, Show segue and Show detail segue seems to do the same thing on iPhones, since there is no much space to present view controllers side by side.

Technically, you don't present details several times until you go back in your navigation. Only the master view controller should perform Show detail segues, a detail view controller should be a leaf in your navigation tree (but it's not forbidden to use a navigationVC as a leaf ;) )

Hope it helps.

presentViewController and displaying navigation bar

It is true that if you present a view controller modally on the iPhone, it will always be presented full screen no matter how you present it on the top view controller of a navigation controller or any other way around. But you can always show the navigation bar with the following workaround way:

Rather than presenting that view controller modally present a navigation controller modally with its root view controller set as the view controller you want:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:myViewController];

//now present this navigation controller modally
[self presentViewController:navigationController
animated:YES
completion:^{

}];

You should see a navigation bar when your view is presented modally.

iOS - pushViewController vs presentModalViewController difference

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"?



Related Topics



Leave a reply



Submit