0
votes

I'm trying to customize the behavior of my application's navigation bar. I've managed to subclass the UINavigationController to run custom code every time I push the back button, yet still, the navigation bar title changes and pressing the button again ignores my code (Maybe because it's and entire different controller?).

What I want to achieve is to tell the UINavigationController to ignore presses to the back button under certain conditions (which are already defined and controller from the custom Navigation Controller), and thus keeping the current view and navigation bar intact.

How can I achieve that? I've only overrode the (UIViewController *)popViewControllerAnimated:(BOOL)animated function.

My code:

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    if(self.auxController == nil)
    {
        NSLog(@"[CustomNavigationController.m]: WARNING: No Auxiliar Controller assigned to Navigation controller");
        return [super popViewControllerAnimated:animated];
    }
    else
    {
        NSLog(@"[CustomNavigationController.m]: Navigation Stack has %d remaining levels",[[[self auxController] parentLayout] count]);
        if([[[self auxController] parentLayout] count] > 0)
        {
            NSLog(@"[CustomNavigationController.m]: Falling to previous navigation level");
            [[self auxController] navBack];
        }
        else
        {
            NSLog(@"[CustomNavigationController.m]: No more navigation levels in stack, falling to previous view");
            self.auxController = nil;
            return [super popViewControllerAnimated:animated];
        }
    }
    return nil;
}
1
Why don't you hide the button rather than disabling the button - vishnuvarthan
Because the button runs some custom code when the user pressed it, so it needs to be visible. I don't want to hide it or disable it, just programmatically ignore the press and then run some code - Imanol Barba Sabariego
You could create a custom Back button (though it would look slightly different) and put it as the left bar item - Stonz2
Well, there's an option. It's unimportant that it looks slightly different for this project so nothing's lost, but I'd still prefer a cleaner solution. If none comes up I will opt for this. Thanks - Imanol Barba Sabariego

1 Answers

1
votes

As @Stonz2 mentioned in the comments, the cleanest solution is using your own custom back button. Just hide the Back button in the Navigation controller and set your own left BarButtonItem. In this BarButtonItem, you can either go back manually, or not do anything.

To speak in code:

UIBarButtonItem * customBackButton = [UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStylePlain  target:self action:@selector(backButtonPressed:)]
navigationItem.hidesBackButton = true;     
navigationItem.leftBarButtonItem = customBackButton;

Don't think there is anything clearer!