我想为我的 UIViewController 添加标题:
我试过了
self.navigationController.navigationItem.title
self.navigationController.title
self.title
有时,解决方案 1 有效,有时解决方案 2 有效,有时解决方案 3 有效。
哪位高手能告诉我它们之间的区别吗?
最佳答案
title
是 UIViewController
的属性。
A localized string that represents the view this controller manages. Set the title to a human-readable string that describes the view. If the view controller has a valid navigation item or tab-bar item, assigning a value to this property updates the title text in those objects.
self.navigationController
是一个 UINavigationController
,它管理 viewController
所在的 viewController 堆栈。UINavigationController
是 UIViewController
的子类,所以 self.navigationController.title
是 UINavigationController
的 title
。
self.navigationItem.title
:
The navigation item’s title displayed in the center of the navigation bar. The default value is nil. When the receiver is on the navigation item stack and is second from the top—in other words, its view controller manages the views that the user would navigate back to—the value in this property is used for the back button on the top-most navigation bar. If the value of this property is nil, the system uses the string “Back” as the text of the back button.
因此,在实践中,您应该设置 ViewController
的 title
。如果您的 ViewController
由 UINavigationController
管理,iOS 会将此标题复制到导航项或标签栏项并在导航栏上显示此标题,当您按下另一个 ViewController
时,它会将该文本用于 Back Button,并且如果您的 ViewController
将它显示在选项卡栏中由 UITabBarController
管理。
关于ios - 标题有什么不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41208404/