本文介绍了navigationController.navigationItem 与 navigationItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是好奇,为什么设置 self.navigationItem = ... 有效,但 self.navigationController.navigationItem 失败?这同样适用于 self.toolbarItemsself.navigationController.toobarItems.

Just curious, why setting self.navigationItem = ... works, but self.navigationController.navigationItem fails? The same applies for self.toolbarItems vs self.navigationController.toobarItems.

什么时候使用self.navigationController.navigationItem?

也许你会说,它们指向不同的东西.但是为什么 self.navigationController.navigationBarHidden = YES 导航栏是隐藏的.不是说 self.navigationController.navigationItem 指向我想要的栏吗?

Maybe you will say, they point to different things. but why self.navigationController.navigationBarHidden = YES the navigation bar is hidden. doesn't it means self.navigationController.navigationItem point to the bar i wanted?

推荐答案

UIViewController有一个属性navigationItem.

无论是 UICollectionViewControllerUITableViewControllerUINavigationViewController 还是任何自定义子类,所有子类也是如此.

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

当由 UINavigationController 呈现时,导航控制器将创建此属性,因此每个视图控制器都有自己的 navigationItem.如果您不从导航控制器中显示它,则 navigationItem 为 nil.

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.

现在,使用 UINavigationController 您很有可能将其用作您的初始视图控制器.因此,导航控制器没有被另一个导航控制器呈现,并且它的 navigationItem 属性为 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.

这篇关于navigationController.navigationItem 与 navigationItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 06:44