UINavigationBar自定义返回按钮

1
[self.navigationController pushViewController:self.bView animated:YES];

一开始想当然的,在B视图的viewDidLoad里直接使用:

1
self.navigationItem.backBarButtonItem.title = @"back";

来更改后退按钮标题,结果后退后,发现A视图的导航栏标题也变成“back”了。

于是网上搜了一下,别人推荐在B视图的viewDidLoad/viewWillAppear里使用:

1
2
3
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backItem];
[backItem release];

我试了,发现无效。

无奈之下,只好研读UINavigationController ClassReference去,在“Updating the Navigation Bar”小节,有这么一段话:

The bar button itemon the left side of the navigation bar allows for navigation backto the previous view controller on the navigation stack. Thenavigation controller updates the left side of the navigation baras follows:

  • If the newtop-level view controller has a custom left bar button item, thatitem is displayed. To specify a custom left bar button item, setthe leftBarButtonItem property of the view controller’s navigationitem.
  • If the top-levelview controller does not have a custom left bar button item,but the navigationitem of the previous viewcontroller has a valid item in itsbackBarButtonItem property,the navigation bar displays that item.
  • If a custom barbutton item is not specified by either of the view controllers, adefault back button is used and its title is set to the value ofthe title property of the previous view controller—that is, theview controller one level down on the stack. (If there is only oneview controller on the navigation stack, no back button isdisplayed.)

我大致解释一下,使用pushViewController切换到下一个视图时,navigationcontroller按照以下3条顺序更改导航栏的左侧按钮。

1、如果B视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个自定义按钮;

2、如果B没有自定义按钮,但是A视图的backBarButtonItem属性有自定义项,则显示这个自定义项;

3、如果前2条都没有,则默认显示一个后退按钮,后退按钮的标题是A视图的标题。

按照这个解释,我把UIBarButtonItem*backItem……这段代码放在A视图的pushViewController语句之前。

OK问题解决了,B视图的后退按钮的标题变成back了。

 

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nilaction:nil]
[self.navigationItem setBackBarButtonItem:backItem];
  [backItem release]
[self.navigationController pushViewController:self.bView animated:YES];
转载自: http://zgia.net/?p=306
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 iOS 开发中的 `UINavigationBar`,可以通过 `UINavigationBarDelegate` 协议来监听导航栏中左侧按钮的点击事件。具体实现步骤如下: 1. 首先在导航栏左侧添加一个按钮,可以使用 `UIBarButtonItem` 的 `initWithImage:style:target:action:` 方法创建,将按钮的 `target` 设置为当前控制器,将 `action` 设置为一个方法,用于处理按钮点击事件。 2. 在当前控制器中实现 `UINavigationBarDelegate` 协议的 `navigationBar:shouldPopItem:` 方法,该方法会在导航栏中的按钮被点击时触发。 3. 在 `navigationBar:shouldPopItem:` 方法中判断当前导航栏中的视图控制器是否为根视图控制器,如果是则返回 NO,否则返回 YES。 4. 在 `shouldPopItem:` 方法中处理完点击事件后,调用 `popViewControllerAnimated:` 方法返回上一个视图控制器。 示例代码如下: ```swift class ViewController: UIViewController, UINavigationBarDelegate { override func viewDidLoad() { super.viewDidLoad() let leftButton = UIBarButtonItem.init(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(leftButtonClicked(sender:))) self.navigationItem.leftBarButtonItem = leftButton } @objc func leftButtonClicked(sender: UIBarButtonItem) { // 处理左侧按钮点击事件 } func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool { if self.navigationController?.viewControllers.count == 1 { return false } return true } } ``` 在上面的代码中,我们首先在 `viewDidLoad` 方法中创建了一个左侧按钮,并将其添加到导航栏中。在 `leftButtonClicked` 方法中我们可以处理左侧按钮的点击事件。 在 `navigationBar:shouldPopItem:` 方法中,我们通过判断当前视图控制器是否为根视图控制器来决定是否返回上一个视图控制器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值