self.title 与 self.navigationItem.title
UIViewController 的 title
属性的用途是什么,标题不能已经用 navigationItem.title
设置吗?
两者似乎都有效,我只是想知道为什么会有这种看似重复的功能。
What is the purpose of the title
property of UIViewController, can't the title already be set with navigationItem.title
?
Both seem to work, I'm just wondering why there's this seemingly duplicated functionality.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
UIViewController 有 title 属性。 在 navigationItem 和 tabBarItem 中都覆盖了 title 属性。 因此,如果我们有带有 tabbar 和 navigationcontroller 的应用程序,并且设置 self.title=@"somethng" 会将标题设置为 navigationTitle 和 tabBartitle。
UIViewController has the title property. in navigationItem and tabBarItem both override the title property. so if we have application with tabbar and navigationcontroller.and setting self.title=@"somethng" will set that title to both navigationTitle and tabBartitle.
在 UIViewController 中,title 和 navigationItem.title 是不同的。 一个例子:如果你在 UITabBarController 中有一个视图控制器(在 NavigationController 中),那么如果你设置 self.title ,它会覆盖选项卡的名称以及顶部标题。 如果您设置 self.navigationItem.title 那么它只会更改顶部标题,而选项卡栏名称保持不变。
In a UIViewController, title and navigationItem.title are different. One example: if you have a view controller (in a NavigationController) in a UITabBarController, then if you set self.title it overrides the name of the tab as well as the top title. If you set self.navigationItem.title then it only changes the top title, leaving the tab bar name unchanged.
视图控制器的标题对于程序员来说既是一种约定,也是一种方便。 调用...
或
确保任何需要检索视图控制器标题的对象(如导航栏)都可以这样做。 使用 navigationItem.title 将允许您根据需要覆盖此标题,但设置控制器的标题可能会被认为更“时尚”。
在我看来,你可以做任何一个,但前者会节省你一些打字;-)
干杯-
The title of a view controller is both a convention and a convenience for you as a programmer. Calling...
or
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-
当您设置
navigationItem.title
时,您正在设置视图顶部的导航栏标题。 当您推送新的视图控制器时,这一点很重要,因为前一个视图的标题(由navigationItem.title
设置)将是后退按钮的文本。 此外,如果您的项目没有导航栏,则navigationItem.title
将不起作用。Apple 对 UIViewController 的
title
属性给出了以下描述。When you set
navigationItem.title
you are setting the title of the navigation bar on the top of the view. This is important when you push a new view controller because the title of the previous view (as set bynavigationItem.title
) will be the text of the back button. Further, if your project does not have a navigation bar,navigationItem.title
will not work.Apple gives the following description for the
title
property of UIViewController.