0

I want to do some saving stuff when the user hits the back button on a navigation controller. Is this only possible by implementing

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    int index = [[self.navigationController.viewControllers] indexOfObject:[self.navigationController.visibleViewController]];
    if(viewController == [[self.navigationController.viewControllers] objectAtIndex:index-1])
        //saving code here

so the delegate gets called when it's about to show the previous view controller. Is there a more elegant way of knowing when the view controller will be popped?

and I can't use viewWillDisappear because there's a button that displays a UIImagePickerController, and I don't want the saving to be done then. Any thoughts?

marty
  • 1,137
  • 2
  • 7
  • 7
  • well you write the code for when a view controller get's popped, so you could do your saving there. And what's not elegant about using delegates? – thelaws Aug 09 '10 at 20:23
  • well i mean that's the only way i can see to do it, but it seems like a common thing to need to do stuff when something gets popped, so i assumed there was another way to do it. there's nothing elegant about the code above haha it just seems like a "cheater" way to get the job done. maybe its just me. – marty Aug 09 '10 at 20:27
  • actually the method above doesn't even work. So the problem stands. – marty Aug 09 '10 at 20:41

2 Answers2

1

Or

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem     *)item
{
  //insert your back button handling logic here
  // let the pop happen
  return YES;
}     
Christian Loncle
  • 1,584
  • 3
  • 20
  • 30
0

You normally do things like that in the "viewWillDisappear:" method of a view controller.

Yes it also will activate if you are going forward, but you can flag that to let the method know if you meant to launch something else - and it's probably a good idea to save no matter what at that point anyway...

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150