11

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?

logancautrell
  • 8,762
  • 3
  • 39
  • 50
석진영
  • 243
  • 4
  • 13

2 Answers2

9

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

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

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

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

Chris Ledet
  • 11,458
  • 7
  • 39
  • 47
Benjie
  • 7,701
  • 5
  • 29
  • 44
1

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

- (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;
}
Vladimir Stazhilov
  • 1,956
  • 4
  • 31
  • 63