navigationController.navigationItem vs navigationItem [英] navigationController.navigationItem vs navigationItem
问题描述
好奇,为什么设置 self.navigationItem = ...
有效,但 self.navigationController.navigationItem
失败?这同样适用于 self.toolbarItems
vs 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
导航时controller将创建此属性,因此每个视图控制器都有自己的 navigationItem
。如果你没有从导航控制器中显示它,那么navigationItem是零。
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
属性为零。
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.
导航bar略有不同,因为这是另一种方式。
The navigation bar is slightly different as this is managed the other way around.
这篇关于navigationController.navigationItem vs navigationItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!