xcode 在导航控制器中单击后退按钮时调用哪个方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7949280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:17:02  来源:igfitidea点击:

Which method is called when back button clicked in navigation controller?

iosxcodecontrollernavigationback-button

提问by ???

I want to save DB when the back button clicked in navigation controller.

我想在导航控制器中单击后退按钮时保存数据库。

so I would insert code in method.

所以我会在方法中插入代码。

What method is called when back button clicked in navigation controller?

在导航控制器中单击后退按钮时调用什么方法?

采纳答案by Benjie

To do what you asked, look at the UINavigationControllerDelegateprotocol, namely the method:

做你问的,看UINavigationControllerDelegate协议,即方法:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 

when the viewController argument is no longer your view controller then you should save.

当 viewController 参数不再是您的视图控制器时,您应该保存。

However, doing so on viewWillDisappear:might be a better (and much simpler) idea.

然而,这样做viewWillDisappear:可能是一个更好(也更简单)的想法。

回答by Vladimir Stazhilov

Maybe it's not appropriate use, but that worked for me. Don't forget to set UINavaigationController delegate.

也许它不适合使用,但这对我有用。不要忘记设置 UINavaigationController 委托。

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC
{
    NSLog(@"from VC class %@", [fromVC class]);
    if ([fromVC isKindOfClass:[ControllerYouJustPopped class]])
    {
        NSLog(@"Returning from popped controller");

    }

    return nil;
}