I cannot seem to find a way to detect when a user presses the back
button in a UINavigationController
. I have tried using the UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
I have tried
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.isMovingFromParentViewController) {
NSLog(@"BACK BUTTON WAS PRESSED!!!!!!!!!");
[[[NSManagedObjectContext MR_defaultContext] undoManager] endUndoGrouping];
[[NSManagedObjectContext MR_defaultContext] undo];
}
}
and I have also tried
- (void)viewDidLoad
{
self.navigationItem.backBarButtonItem.target = self;
self.navigationItem.backBarButtonItem.action = @selector(cancelPressed);
}
- (void)cancelPressed
{
NSLog(@"CANCEL BUTTON WAS PRESSED!!!!!!!!!");
[[[NSManagedObjectContext MR_defaultContext] undoManager] endUndoGrouping];
[[NSManagedObjectContext MR_defaultContext] undo];
}
The first 2 work great however, when I press Done
I run an AFNetworking
call then onComplete
of that I do a [[NSNotificationCenter defaultCenter] postNotificationName:]
and then the parentViewController
runs a few things and refreshes the screen and pops the child view controller. Upon this child view controller popping, the first 2 also run.
The last one I mentioned doesn't work at all. Is there a proper way to check if the back
button of a navigationController was pressed that will run for the back button only and not just when the view controller is popped?