user6528991
user6528991

Reputation:

Difference between using UINavigationController and UINavigationBar

I started learning ios development recently. I made a project using UINavigationController by embedding it because I need a title bar. But I can achieve the same thing by using UINavigationBar from the object library.

The only difference I saw is when I use UINavigationController I get a back button when I use segue. Are there any more differences between them? Which one is used in which situations?

By the way, why another NavigationController is added to my storyboard? What is the significance of it?(No one explained about it in any video I have seen)

Thank you.

Upvotes: 6

Views: 2137

Answers (2)

Reinier Melian
Reinier Melian

Reputation: 20804

The NavigationController handles its own UINavigationBar automatically and also handles your navigation stack of viewControllers, The NavigationController has a delegate that you can use to know what is happening in your viewControllerNavigationStack and so forth

Using the delegate of NavigationController you can for example be notified when:

  • The NavigationController will show some viewController, using this method :

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

  • The NavigationController did show some viewController, using this method :

    - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
    

You can also use NavigationController to go into specific viewController on your navigation stack for instance, or you can pop and push viewController without segues

Upvotes: 4

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

The UINavigationBar:

The UINavigationBar class provides a control for navigating hierarchical content. It’s a bar, typically displayed at the top of the screen, containing buttons for navigating within a hierarchy of screens. The primary properties are a left (back) button, a center title, and an optional right button. You can use a navigation bar as a standalone object or in conjunction with a navigation controller object.

The UINavigationController:

The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This navigation interface makes it possible to present your data efficiently and makes it easier for the user to navigate that content. You generally use this class as-is but you may also subclass to customize the class behavior.

Using a Navigation Bar With a Navigation Controller

The most common way to use a navigation bar is in conjunction with a UINavigationController object. If you use a navigation controller to manage the navigation between different screens of content, the navigation controller creates the navigation bar automatically and pushes and pops navigation items when appropriate.

A navigation controller automatically assigns itself as the delegate of its navigation bar object. Therefore, when using a navigation controller, don’t assign a custom delegate object to the corresponding navigation bar

enter image description here

Upvotes: 2

Related Questions