Navigationcontroller.Navigationitem VS Navigationitem

navigationController.navigationItem vs navigationItem

The class UIViewController has a property navigationItem.

This is true of all the subclasses too whether it is a UICollectionViewController, UITableViewController, UINavigationViewController or any custom subclass.

When presented by a UINavigationController the nav controller will create this property and so each view controller gets its own navigationItem. If you do not present it from a navigation controller then the navigationItem is nil.

Now, with a UINavigationController you are more than likely using this as your initial view controller. Therefore, the navigation controller is NOT being presented by another navigation controller and its navigationItem property is nil.

The navigation bar is slightly different as this is managed the other way around.

Navigation Controller, Navigation Item and Navigation Bar

The navigation item manages the buttons (left and right) and views (mainly the title view) to be displayed in a navigation bar.
Each navigation item points to a specific view controller.

The navigation bar is the container for the navigation item and mainly handles layout, transitions and animations.

You can read more about them in the official docs:

https://developer.apple.com/documentation/uikit/uinavigationitem

https://developer.apple.com/documentation/uikit/uinavigationbar

What's the difference between UINavigationController and UINavigationItem

I believe what you are asking is the following: self.navigationController.navigationItem and self.navigationItem. I am ignoring the rest of the question since it is not really essential to the question.

From what I understand self.navigationController.navigationItem is useless since it is accessing navigation item of the navigation controller (ie your rootViewController).What you really want is self.navigationItem, navigation item of the view controller because that is what essentially going to show up in your view controller.

UINavigationController is a subclass of UIViewController so self.navigationController.navigationItem is just a spillover method from subclassing. It does not do anything positive (at least in my experience).

Edit: Read this for further clarification.

Difference between self.navigationController.navigationItem and self.navigationItem

A UINavigationController is a subclass of UIViewController. As such, it has its own independent navigationItem property, which it inherited from UIViewController. You should ignore this property, as it would only be used if you were to embed a navigation controller inside another navigation controller (which nobody in their right mind would ever do).

why there is a navigationItem in the UIViewController?

The point is that all UIViewControllers have a UINavigationItem.

A UINavigationController decides what to show in its navigationBar by looking at the current viewController's navigationItem.

So you get this;

vcA.navigationItem.title = "A"
vcB.navigationItem.title = "B"

navigationController = UINavigationController(rootViewController: vcA)
// The title in the navigationBar is now "A"

navigationController.pushViewController(vcB, animated: true)
// The title in the navigationBar is now "B"

Since UINavigationController is also a subclass of UIViewController, it inherits the navigationItem as well, even though it's useless in most cases.

self.navigationItem.rightBarButtonItem vs self.navigationController.navigationItem.rightBarButtonItem

The first, i.e. self.navigationItem.rightBarButtonItem.

Every view controller has an associated navigation item. This is what is displayed in a navigation bar. So it's the current view controller's navigation item that you want to manipulate. If you manipulated the navigation controller's navigation item then that would show if the navigation controller (also a view controller) were itself shown inside another navigation controller.

What's the different with titles?

title is a property of UIViewController.

A localized string that represents the view this controller manages.
Set the title to a human-readable string that describes the view. If
the view controller has a valid navigation item or tab-bar item,
assigning a value to this property updates the title text in those
objects.


self.navigationController is a UINavigationController that manages the stack of viewControllers that your viewController is in. UINavigationController is a subclass of UIViewController, so self.navigationController.title is the title of the UINavigationController.


self.navigationItem.title:

The navigation item’s title displayed in the center of the navigation
bar. The default value is nil. When the receiver is on the navigation
item stack and is second from the top—in other words, its view
controller manages the views that the user would navigate back to—the
value in this property is used for the back button on the top-most
navigation bar. If the value of this property is nil, the system uses
the string “Back” as the text of the back button.


So, in practice, you should set the title of your ViewControllers. iOS will copy this title to the navigation item or tab bar item and display this title on the Navigation Bar if your ViewController is managed by a UINavigationController, it will use that text for the Back Button when you push to another ViewController, and it will display it in the tab bar if your ViewController is managed by a UITabBarController.

How to set NavigationController navigationItem title from ViewController loaded by Xib

Adding a UINavigationBar view to the main UIView doesn't do what you think it does. It merely adds a navigation bar view there in the view, it does not tie into the UINavigationController.

If you use a storyboard, you can add the UINavigationItem as a child of the UIViewController. But since you are not using a storyboard, the easiest way to do it is to update the navigationItem in viewDidLoad.


Sidenote: Apple does not recommend using viewDidLoad to setup the navigation item. If you really want to do it the proper way, you can for example do it in the navigationItem getter.

- (UINavigationItem*)navigationItem
{
// get super navigation item
UINavigationItem *navItem = [super navigationItem];

// do stuff

return navItem;
}

Setting navigation item title issue.. why my navigation item seems many?

Now View controllers are:

ViewController1 attached to item1:

override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title="View1"

}

ViewController2 attached to item2:

override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title="View2"

}

Sample Image

Result:

Sample Image

Sample Image

self.title vs self.navigationItem.title

The title of a view controller is both a convention and a convenience for you as a programmer. Calling...

self.title = @"Some Title";

or

[self setTitle:@"Some Title"];

ensures that any object (like the navigation bar) that needs to retrieve the title of your view controller can do so. Using navigationItem.title will allow you to over-ride this title as needed but it may be consider more "stylish" to set the title of your controller instead.

IMO you can do either but the former will save you some typing ;-)

Cheers-



Related Topics



Leave a reply



Submit