设置导航控制器中后退按钮的操作 [英] Setting action for back button in navigation controller

查看:105
本文介绍了设置导航控制器中后退按钮的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖导航控制器中后退按钮的默认操作。我为自定义按钮提供了一个操作的目标。奇怪的是,当通过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];
}

这篇关于设置导航控制器中后退按钮的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆