作者
fever105
2015.07.25 12:09*
写了67640字,被112人关注,获得了56个喜欢
探究 - UIViewController的title属性和其navigationItem的title属性之间的区别
字数488
阅读831
评论1
喜欢2
探究 - UIViewController的title属性和其navigationItem的title属性之间的区别
UIViewController 有两个title 属性,一个是其本身的,另一个是其navigationItem 属性所指向的UINavagtionItem 对象的title 属性。
定义比较
首先比较这两个title 属性的定义:
-
UIViewController 的title :
A localized string that represents the view this controller manages.
一个代表这个视图控制器所管理的视图的本地字符串。
-
UINavagtionItem 的title :
The navigation item’s title displayed in the center of the navigation bar.
显示在导航栏中间位置的导航项目的标题。
用法比较
从定义上来看,似乎控制器的title (以下简称ctitle )只是用来指代其所管理的视图的,就好像人名一样。而UINavigationItem 的title (以下简称ntitle )会显示在导航栏的中间,就好像当前导航内容的名片一样。
在下面的例子中,创建一个UINavigationController ,将其根视图控制器设置为一个UITableVieController ,然后分别设置根视图控制器的ctitle 和ntitle 属性。查看导航栏中的显示效果,同时向控制台输出ctitle 和ntitle ,看它们之间是否存在联系。
-
情况1,将
ntitle 设置为self.navigationItem.title ,不设置ctitle 。
![](http://image98.360doc.com/DownloadImg/2016/08/0409/77180167_2.png) 代码和控制台输出结果
![](http://image98.360doc.com/DownloadImg/2016/08/0409/77180167_3.png) 导航栏效果
-
情况2,将
ctitle 设置为self.title ,不设置ntitle 。
![](http://image98.360doc.com/DownloadImg/2016/08/0409/77180167_4.png) 代码和控制台输出结果
![](http://image98.360doc.com/DownloadImg/2016/08/0409/77180167_5.png) 导航栏效果
-
情况3,将
ctitle 设置为self.title ,ntitle 设置为self.navigationItem.title 。请注意设置顺序。
![](http://image98.360doc.com/DownloadImg/2016/08/0409/77180167_6.png) 代码和控制台输出结果
![](http://image98.360doc.com/DownloadImg/2016/08/0409/77180167_7.png) 导航栏效果
-
情况4,将
ntitle 设置为self.navigationItem.title ,ctitle 设置为self.title 。请注意设置顺序。
![](http://image98.360doc.com/DownloadImg/2016/08/0409/77180167_8.png) 代码和控制台输出结果
![](http://image98.360doc.com/DownloadImg/2016/08/0409/77180167_9.png) 导航栏效果
结论
通过以上比较得知,ctitle 和ntitle 的关系类似于父集与子集。当设置ctitle 时,会同时设置ntitle ;当设置ntitle 时,ctitle 为空,如果此后再设置ctitle ,则为覆盖ntitle 。
|