UINavigationController vs viewController embed in NavigationController - ios

I was trying my hands on iOS and while building apps, one question get into mind that what is the difference between UINavigationController vs viewController embed in NavigationController.
While using UINavigationController we push and pop views.
while using viewController we present and dismiss.
SO what are the applications where one is more superior to use than the other.

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.
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

I think you are really describing two sides to the same coin. There is only one way to use a UINavigationViewContorller. It is a known as a container view controller and its job is to push and pop other UIViewControllers. A UINavigationViewController works with viewControllers, not views.

UINavigationController:
If you have hierarchy of view controller then that is you have stack of view then you need to use navigation controller. You can perfrom push and pop operations on the view controller and Navigation Controller is the rootViewController of all ViewController.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.
UIViewController: if you are using view controller it act as presentViewController. The 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. I think that presentViewController is most suitable for use with just one view controller being presented at a time. So you simply will not be able to implement a "go back"/navigation like functionality.

UINavigationController inherits from UIViewController. The strange thing about this object model is the UIViewController has a property called NavigationController. So for OO purists this is a bit baffling that a parent class knows about its children. But moaning aside this is how it’s been done in UIKit. As you’ll find with a runtime error, you can’t place a UINavigationController inside a UINavigationController
Whenever you push a controller, it can access the parent UINavigationController it may or may not belong to via the NavigationController property. The property is null if the controller is not inside a UINavigationController.

Related

Launching to a level other than root in UINavigationController, from a storyboard

Is it possible to launch an app to a specific level of a navigation stack using a storyboard?
I'm looking to recreate the model employed by Mail.app, where the app launches into the Inbox, but this is actually one level down the navigation stack, and tapping the back button takes you to the root...
I understand how this can be done via code, i.e. instantiating the navigation controller within the app delegate and then manually pushing the view controller(s) to create the desired stack, but I'd really like to know if there's a way to achieve the same using storyboards.
Unfortunately I don't think there is because you need to instantiate your navigation controller at some point that will house your view controllers, and if you do this through storyboards the best you can do is set the navigation controller to be the entry point.
However, it is pretty straightforward to do from code. If your navigation controller has two view controllers where ViewControllerOne pushes to ViewControllerTwo, then you can just can just push to the second one without an animation as follows:
navigationController.pushViewController(secondViewController, animated: false)
And the user will be one level deep in the navigation controller.

Functional difference, UINavigationController vs Only Storyboard Segue

So UIKit Framework Reference describes UINavigationController as an efficient way to present data and views to user. But I'm curious as to the performance and manageability differences between using UINavigationController VS SegueOnly.
There must be certain situations that one is better-suited than the other. I am entirely new to coding but instinctually I imagine UINavigationController is more for presenting VC's that each have a lot going on and user spends much time on each VC equally or that multiple VC's need to be running and holding data at same time. And that Segue are more for Uni-directionally VC progression or short-use, low-data VC presentations. Are any of these assumptions correct?
What are the PROS to implementing a UINavigationController?
This is an Apples to Socket-wrenches comparison. A navigation controller versus a segue isn't even an option. They don't fill the same role. You can use a navigation controller with or without using segues. You can use segues with or without a navigation controller.
A UINavigationController is a specific type of view controller. Specifically, it is designed for controlling a navigation stack of view controllers. When a user moves forward through your app, you add other view controllers to this navigation stack (either through segues or through other methods available on the view controller to present other view controllers). Meanwhile, the navigation controller allows you to "pop" controllers off the stack and we go backward to the previous view controller.
When we're comparing a UINavigationController to things, we should compare it to things like UITabBarController, or UISplitViewController, or UIPageViewController. These are other types of view controllers that control the way in which a user navigates through our app.
Segues exist as a means to get from one view controller to another. But the various controllers I just listed exist as a means for controlling the navigational relationship between your controllers.
If you are using storyboards for building your app, I highly recommend that you use segues. Whether or not you use a navigation controller is entirely up to the way you want your application navigation to work.
Perhaps some pictures might make the distinction more clear?
Exhibit A
Here we have two view controllers. No navigation controller, nothing. Just two normal controllers. The line with the arrow between the view controllers is the segue. The segue defines a relationship between the view controllers which lets us get from the one on the left to the one on the right.
Exhibit B
Here we have a navigation controller, followed by two view controllers. The line between the navigation controller (far left) and the first view controller (middle) is not a segue. However, it does define a relationship between the navigation controller and the view controller. It defines the view controller as the navigation controller's root view controller. Whenever we present this navigation controller, it will in turn present this view controller as the first controller on its navigation stack. The line between the first view controller (middle) and the second view controller (far right), however, is a segue. It's identical to the segue from exhibit A (except exhibit A is probably a different type of segue). This segue defines a relationship between the first and second view controller and provides us with a means of getting from first to second.
Exhibit C
Here we have a tab bar controller (far left) and four view controllers. We also have a bunch more lines connecting these things. Much line in the navigation controller example, the lines between the tab bar controller and the view controllers are NOT segues, but they do define a relationship between the tab bar controller and the view controllers. These lines assign the view controllers as part of the tab bar controller's viewControllers array, which determines what tabs to display. The lines between the left view controllers and the right view controllers, however, are segues, exactly the same as the first two examples.
A segue is nothing more than a way of defining a navigational relationship between two view controllers on your storyboard. If you weren't using a storyboard, you also wouldn't be using segues at all, but you could still make your app navigate in the exact same manner (this can all be achieved programmatically without the storyboard and without segues--and I don't think it effects performances on way or another).
Whether or not to use a navigation controller is another question and an entirely separate question at that. Whether or not to use a navigation controller isn't even remotely in the same ballpark to whether or not you're using segues. Using a navigation controller also isn't a question of performance. The overhead for using a navigation controller is extraordinarily minor. Most of the overhead for it probably comes from the UI stuff it adds... but if you want that UI stuff, you're going to have that overhead whether or not you have a navigation controller.
Importantly, whether or not to use segues isn't a question of performance--it's merely a question of whether or not you're designing your app using storyboards. And equally importantly, using navigation controllers isn't a question of performance--it's a question of how you want your app's navigation to look and feel. "Is a navigation controller the right look and feel for your app?" is literally the only question you have to answer when deciding whether or not to use a navigation controller.

What are limitations of presentViewController over UINavigationController

I am confused about "In which situations we have to use presentViewController and UINavigationController".
I had read so many documents, but I haven't found accurate explanation. We can always use UInavigationController then what is the use of presentViewController ?
Thanks.
UINavigationController maintains the stack of the controllers that are being viewed. So once you push through 1->2->3 view controllers then you can pop in 3->2->1 manner. Unless you don't change the stack this kind of flow is maintained by UINavigationController. Now lets say you want to show 4th view controller without disturbing the above flow. Then you can use presentViewController.
This is the simplest and basic is for using navigation controller and presentViewController
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

Use of differenct view controllers

i'm curious about what's the best way to plan the controllers for my app.
i want my main screen to have 3 button.
1) should open a nav controller with details view
2) should open a controller with other buttons that lead to others controllers
3) should open a tab bar with 2 pages ( or eventually use a switch to change page instead of the tab bar)
this is the schema of what i want
http://i59.tinypic.com/2rrvrd4.png
Is it a correct schema or i should use my controllers differently? will apple reject an apple with such schema?
thanks
As #Fogmeister pointed out in the comments, going for a UITabBarController as the main interface for your app actually seems to be a more appropriate solution here.
However, you can go with the interface that you described, but then you should keep in mind that with your current setup, you are not only using UINavigationController in the first case, but your whole navigation system is still built upon UINavigationController in the following way:
Your app has one instance of UINavigationController.
Your initial UIViewController (the one with the three buttons), is the rootViewController of your UINavigationController.
You can navigate to the other view controllers using [self.navigationController pushViewController:newViewController] (or performSegue if you prefer using Storyboards).
In the case of your third view controller, you are pushing a UITabBarController onto the navigation controller's view controller stack, this UITabBarController needs to be initialized with the two view controllers that it is going to display before it gets pushed onto the stack.

difference between presentViewController and UINavigationController?

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?
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.
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.
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.
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 +

Resources