Difference between self.navigationController.navigationItem and self.navigationItem

What is the difference between self.navigationController.navigationItem and self.navigationItem? I use UINavigationController throughout the application, and when I use self.navigationController.navigationItem.rightBarButtonItems, in one of the viewDidLoad methods of one of the viewControllers the array is empty. However, if I use self.navigationItem.rightBarButtonItems, I see the elements of the bar panel. What is the difference between the two calls? Thank!

+5
source share
2 answers

A UINavigationControlleris a subclass UIViewController. Thus, it has its own independent property navigationItem, which it inherited from UIViewController. You should ignore this property, because it will only be used if you have to embed the navigation controller in another navigation controller (which no one in their right mind would ever do).

+5
source

From the documentation UIViewController+UINavigationControllerItem, see below, we should use the property navigationItemdirectly on the view controller:

// Created on-demand so that a view controller may customize its navigation appearance.
@property(nonatomic,readonly,strong) UINavigationItem *navigationItem;

UINavigationControlleralso has a property navigationItem, since it inherits from UIViewController, but setting it will not affect the navigation properties of the view controller.

From a quick test, we see that these navigation elements are not equal:

(lldb) po self.navigationItem
<UINavigationItem: 0x7f865c99ec50>

(lldb) expr -- @import UIKit
(lldb) po self.navigationController.navigationItem
<UINavigationItem: 0x7f865c811740>
0
source

All Articles