给ViewController添加BarButton
来源:互联网 发布:申请淘宝达人的步骤 编辑:程序博客网 时间:2024/04/28 17:37
用代码给UIViewController(self)添加BarButton时,下面的代码不起作用:
self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)];
而这样就可以:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)];
关于UINavigationItem
,苹果官网文档:
This is a unique instance of UINavigationItem created to represent the view controller when it is pushed onto a navigation controller. The first time the property is accessed, the UINavigationItem object is created. Therefore, you should not access this property if you are not using a navigation controller to display the view controller. To ensure the navigation item is configured, you can either override this property and add code to create the bar button items when first accessed or create the items in your view controller’€™s initialization code.
Avoid tying the creation of bar button items in your navigation item to the creation of your view controller’€™s view. The navigation item of a view controller may be retrieved independently of the view controller’€™s view. For example, when pushing two view controllers onto a navigation stack, the topmost view controller becomes visible, but the other view controller’€™s navigation item may be retrieved in order to present its back button.
The default behavior is to create a navigation item that displays the view controller’€™s title.
刘大帅在他的博客【iOS】导航栏那些事儿详细解释了原因:
事实上,UINavigationController并没有navigationItem这样一个直接的属性,由于UINavigationController继承于UIViewController,而UIViewController是有navigationItem这个属性的,所以才会出现如图所示的情况。
文/刘大帅(简书作者)
原文链接:http://www.jianshu.com/p/f797793d683f#
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
还举了个很形象的栗子:
如果把导航控制器比作一个剧院,那导航栏就相当于舞台,舞台必然是属于剧院的,所以,导航栏是导航控制器的一个属性。视图控制器(UIViewController)就相当于一个个剧团,而导航项(navigation item)就相当于每个剧团的负责人,负责与剧院的人接洽沟通。显然,导航项应该是视图控制器的一个属性。虽然导航栏和导航项都在做与导航相关的事情,但是它们的从属是不同的。
我想,这个类比应该能解决以上的疑惑吧。导航栏相当于负责剧院舞台的布景配置,导航项则相当于协调每个在舞台上表演的演员(bar button item,title 等等),每个视图控制器的导航项可能都是不同的,可能一个右边有一个选择照片的bar button item,而另一个视图控制器的右边有两个bar button item。
文/刘大帅(简书作者)
原文链接:http://www.jianshu.com/p/f797793d683f#
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
- 给ViewController添加BarButton
- Navigation上的BarButton添加动画效果
- IOS开发 如何给ViewController添加stroyboard entry pointer?
- 给ViewController减肥
- 添加子VIewcontroller
- ViewController 删除 添加 输入
- ios客户端学习-如何在storyboard中给一个viewController添加navigationController
- 在viewController“添加”viewController的方法
- 在ViewController中添加n个子ViewController
- iOS给Navigation的BarButton的加消息提醒角标
- iOS给Navigation的BarButton的加消息提醒角标
- iOS 之在一个viewController中添加多个viewController
- 给UITabbarcontroller直接add子viewcontroller
- ScrollView上添加多个ViewController
- 【UIKit】-13-UIBarButtonItem - barButton
- ViewController
- ViewController
- ViewController
- Android详细的对话框AlertDialog.Builder使用方法
- 3-4 计算长方形的周长和面积
- [f.lux] linux中的夜间模式
- mysql 多表关联查询
- HDU-2159-FATE(二维完全背包)
- 给ViewController添加BarButton
- PHP学习总结(14)——PHP入门篇之常用运算符
- C Primer Plus学习 三十七 具有外部链接的静态变置
- java中Map,List与Set的区别
- No 'Access-Control-Allow-Origin' header is present on the requested resource.
- vagrant box配置本地开发环境以及常见错误解决方案
- 什么是SOA,谈谈自己的看法(转)
- 【并行计算-CUDA开发】浅谈GPU并行计算新趋势
- mysql性能优化