0

I want to prompt the user to save changes before they navigate back out of editing an entity in my IOS7 application.

An early stack overflow said I can detect this will happen at this point here: but I've been unable to abort the navigation.

-(void)willMoveToParentViewController:(UIViewController *)parent {
NSLog(@"\t\t\tThis VC has has been pushed popped OR covered");

if (!parent) {
    NSLog(@"\t\t\tThis happens ONLY when it's popped");

}

}

basically if my managedObjectContext has unsaved changes I'd like to pop an alert box before the back navigation event happens. Any ideas?

I tried this solution: https://stackoverflow.com/a/19210888/2069812 but doesn't seem to work.

Community
  • 1
  • 1
Jeef
  • 25,082
  • 16
  • 71
  • 136

3 Answers3

1

You can use custom back button and handle its touch event yourself. It's the simplest solution in this case.

For example:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Button" style: UIBarButtonItemStylePlain target:self action:@selector(buttonDidTouch:)];

- (void)buttonDidTouch:(id)sender)
{
    if (edited) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        // Do your stuffs
    }
}
ninjapro
  • 107
  • 5
  • Please add further details when answering a question to make it easier for the OP and future users to understand. – Deepend Jul 15 '14 at 16:23
  • Sorry for that, added example – ninjapro Jul 15 '14 at 16:30
  • No problem. Its full authoritative answers which make this site useful. its easier for people to quickly decide if the answer is useful to them or not. – Deepend Jul 15 '14 at 16:39
0

I know my answer is not using -(void)willMoveToParentViewController:(UIViewController *)parent, but the easiest would be to just display no back button.

This can be done via:

self.navigationItem.hidesBackButton = YES;

You can also disable the swipe gesture as described here:

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

But if you really want to hack in there then I would try a custom back button and handle the action in the controller.

Community
  • 1
  • 1
lukaswelte
  • 2,623
  • 1
  • 18
  • 43
  • But I still need a back button... how do you suggest i go about that? – Jeef Jul 15 '14 at 16:11
  • edited my answer with a try, but I'm not sure if this can work.. cause usually these delegate methods are just invoked to react to it in order to save this data for example. – lukaswelte Jul 15 '14 at 16:13
  • I'll give that a shot... it looks like you are trying to hack the navStack and push yourself back on? – Jeef Jul 15 '14 at 16:13
  • Yes, that would be my try. There is no nice API for interfering this pop back of the navigation controller – lukaswelte Jul 15 '14 at 16:14
  • The other possibility would be setting a custom back button in the navbar. This would enable you to receive the action of the button and react like you want it to. – lukaswelte Jul 15 '14 at 16:17
  • @lukaswelte, the only issue with hiding the back button is that the user still can swipe back to recent view controller in iOS7+. – holex Jul 15 '14 at 16:27
0

I just made a custom button in IB and attached to it. Issue solved.

Jeef
  • 25,082
  • 16
  • 71
  • 136