Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController Custom popViewControllerAnimated Method

I have made a custom UINavigationController class so that I can have a UIAlertView popup and say, are you sure you want to leave this view when tapping the back button.

I have a customer the method like so below:

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    if([[self.viewControllers lastObject] class] == [weddingSetupController class]){

        UIAlertView *exitAlert = [[UIAlertView alloc] 
                                  initWithTitle:@"Are you sure?" 
                                  message:@"By leaving the setup, all current changes will be lost. The setup can be retsrated later if you decide to leave now. However, it is recomened for your best experience that you complete the setup." 
                                  delegate:self cancelButtonTitle:@"No" 
                                  otherButtonTitles:@"Yes", nil
                                  ];

        [exitAlert show];

        return nil;
    } 
    else {
        return [super popViewControllerAnimated:animated];
    }
}

It works well, although an issue is, that while if I say 'No', it stays on the same view, not popping the parent view, the navigation bar does pop. So I get the view not to pop but the bar always returns to the state of its parent view.

like image 684
Josh Kahane Avatar asked Mar 31 '26 02:03

Josh Kahane


1 Answers

When you press the back button on the navigation bar, it's calling:

- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;

in UINavigationBar, which in turn calls popViewControllerAnimated on the navigation controller. Override this method (I use a category, to avoid subclassing) and take your same approach. Your overriding point was just a step late.

like image 77
Jacob Jennings Avatar answered Apr 02 '26 21:04

Jacob Jennings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!