Confirm Back Button on Uinavigationcontroller

Confirm back button on UINavigationController

Unfortunately, you can't intercept the back button in this way. The closest facsimile is to use your own UIBarButtonItem set to the navigationItem.leftBarButtonItem and set an action to display your alert etc. I had a graphic designer create button images that look like the standard back button.

As an aside, I needed to intercept the back button for a different reason. I urge you to reconsider this design choice. If you are presenting a view where users can make changes and you want them to have the choice to save or cancel IMHO it's better to use 'Save' and 'Cancel' buttons vs a back button with an alert. Alerts are generally annoying. Alternatively, make it clear that the changes your users are making are committed at the time they make them. Then the issue is moot.

UINavigationController: How to cancel the back button event?

viewWillDisappear is a delegate method for the event that the view is going to disappear - and there's nothing the developer can do about that! If you could, it would be a viewShouldDisappear delegate method.

So I guess the only way is as you suggest, to use a custom leftBarButtonItem.

Detect if default back button in navigation bar is pressed

In your viewWillDisappear method, add the isMovingFromParentViewController property:

if self.isMovingFromParentViewController {
//do stuff, the back button was pressed
}

EDIT: As had pointed out, if you are merely dismissing the view controller when the save button is pressed, you need a Bool value to check if the save button was pressed or the default back button.

At the top of your class, define a Boolean value:

var saveButtonPressed = false

I'm assuming that you have an @IBAction method linked to your save button. If you don't, then I suggest you add that connection.

In that method, set saveButtonPressed equal to true.

Then, in your viewWillDisappear method, add this to the if condition:

if (self.isMovingFromParentViewController && !saveButtonPressed) {
//do stuff, the back button was pressed
}

back button callback in navigationController in iOS

William Jockusch's answer solve this problem with easy trick.

-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
}

Prompting an UIAlertView to user before navigating back to the previous controller in a UINavigationController stack

I have a simple solution, that has worked perfectly for me without using any third-party component.

#pragma mark - Configuration -

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

self.navigationItem.titleView = self.titleView;
self.navigationController.navigationBarHidden = NO;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"red_cross"] style:UIBarButtonItemStyleBordered
target:self action:@selector(confirmPopViewController:)];
}

Setup your viewController to use your own navigationItem with a selector.

#pragma mark - Events -

- (void)confirmPopViewController:(id)sender
{
[[[UIAlertView alloc] initWithTitle:@"My ViewController" message:@"Do you really want to close ?" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"Yes", nil] show];
}

Implement the event and create/show a your UIAlertView There.

#pragma mark - Delegates -
#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex)
[self.navigationController popViewControllerAnimated:YES];
}

Finally, with the UIAlertViewDelegate, check if the choice of your user and respond accordingly.

Here when the user confirm we popViewControllerAnimated or we do nothing.

UINavigationController back button title inconsistent?

I've noticed a similar behavior that if the back button's title length won't fit in the Navigation Bar with the title of the pushed VC, the Back Button's title will fallback to just < Back.

On the VCs where the inconsistent behavior is happening, try setting the title of pushing VC to a shorter word just to test and confirm if that is indeed what's happening. Hope this helps.



Related Topics



Leave a reply



Submit