开发者

How to perform action when navigation controller pops a view on iPhone?

开发者 https://www.devze.com 2023-01-10 17:07 出处:网络
I want to do some saving stuff when the user hits the back button on a navigation controller.Is this only possible by implementing

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 *)viewC开发者_Go百科ontroller 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?


Or

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem     *)item
{
  //insert your back button handling logic here
  // let the pop happen
  return YES;
}     


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消