Reputation: 115
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;
}
Upvotes: 0
Views: 225
Reputation: 5671
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!
Upvotes: 1