Setting action for back button in navigation controller(在导航控制器中设置后退按钮的操作)
问题描述
我正在尝试覆盖导航控制器中后退按钮的默认操作.我在自定义按钮上提供了一个目标操作.奇怪的是,当通过 backbutton 属性分配它时,它不会注意它们,它只是弹出当前视图并返回到根:
I'm trying to overwrite the default action of the back button in a navigation controller. I've provided a target an action on the custom button. The odd thing is when assigning it though the backbutton attribute it doesn't pay attention to them and it just pops the current view and goes back to the root:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle: @"Servers"
style:UIBarButtonItemStylePlain
target:self
action:@selector(home)];
self.navigationItem.backBarButtonItem = backButton;
只要我通过 navigationItem
上的 leftBarButtonItem
设置它,它就会调用我的操作,但是按钮看起来像一个普通的圆形按钮,而不是带箭头的返回按钮:
As soon as I set it through the leftBarButtonItem
on the navigationItem
it calls my action, however then the button looks like a plain round one instead of the arrowed back one:
self.navigationItem.leftBarButtonItem = backButton;
如何让它在返回根视图之前调用我的自定义操作?有没有办法覆盖默认的后退操作,或者有没有在离开视图时总是调用的方法(viewDidUnload
不这样做)?
How can I get it to call my custom action before going back to the root view? Is there a way to overwrite the default back action, or is there a method that is always called when leaving a view (viewDidUnload
doesn't do that)?
推荐答案
试着把它放到你想要检测压力的视图控制器中:
Try putting this into the view controller where you want to detect the press:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
}
这篇关于在导航控制器中设置后退按钮的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在导航控制器中设置后退按钮的操作
基础教程推荐
- UINavigationBar 隐藏按钮文本 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- iOS4 创建后台定时器 2022-01-01