navigationController.navigationItem vs navigationItem(navigationController.navigationItem 与 navigationItem)
问题描述
只是好奇,为什么设置 self.navigationItem = ...
有效,但 self.navigationController.navigationItem
失败?这同样适用于 self.toolbarItems
与 self.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
.
无论是 UICollectionViewController
、UITableViewController
、UINavigationViewController
还是任何自定义子类,所有子类也是如此.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:navigationController.navigationItem 与 navigationItem
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01