如何设置backBarButtonItem的title和action


1、为什么在UIViewController内设置了self.navigationItem.backBarButtonItem 对于导航栏显示的backBarButtonItem不起任何作用?


首先我们得了解一下

backBarButtonItem

leftBarButtonItem

rightBarButtonItem


他们都属于UINavigationItem的组成部分,都显示在navigationBar上,都属于UIBarButtonItem类


backBarButtonItem和另外两兄弟是有区别的

比如当前有AController准备push到BController,设置backBarButtonItem的title和image需要在AController内设置,在调用AController Push:B之前进行设置,AController.navigationItem.backBarButtonItem = ....


而其他两兄弟则是在BController的ViewDidload后设置均可.


backBarButtonItem的描述

Discussion

When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. When this property is nil, the navigation item uses the value in its title property to create an appropriate back button. If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.

说明了backBarButtonItem只能自定义image和title,不能重写target or action,系统会忽略其他的相关设置项。

如果硬是需要重写action做一些其他的工作,则需要自定义一个leftBarButtonItem,因为系统定义leftBarButtonItem的显示优先级比backBarButtonItem优先级高,当存在leftBarButtonItem时,自动忽略backBarButtonItem,达到重写backBarButtonItem的目的。



2、各个对象下的backBarButtonItem的区别

对于这3兄弟,在3个类下面都能发现他们


比如当前在一个UIViewController内,输入以下方法都能发现他们。(同leftBarButtonItem | rightBarButtonItem

self.navigationItem.backBarButtonItem

self.navigationController.navigationItem.backBarButtonItem

self.navigationController.navigationBar.backItem.backBarButtonItem


比如在AController->BController,在A设置了self.navigationItem.backBarButtonItem,经过试验发现,这个backBarButtonItem为BController的self.navigationController.navigationBar.backItem.backBarButtonItem。


UIViewController的属性navigationItem正是被当前UINavigationBar--[UINavigationBar appearance]管理的属性

 
@property(nonatomic, readonly, retain) UINavigationItem *navigationItem

The navigation item used to represent the view controller in a parent’s navigation bar. (read-only)


self.navigationController.navigationItem.backBarButtonItem

则是表示当前navigationController的parent的UINavigationBar,一般情况下没有这样的嵌套。



分享请注明出处:objs-张渝的原创博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值