ylliX - Online Advertising Network

UINavigationItem back button in UIViewController hell

Sooner or later you will face with changing back button title in navigation bar. This may seem simple. But after few hours trying to change this damn text, you will change your mind. I have no idea why this is designed this way, but answer for question “How can I change back button text in current view controller?” is – you cannot. So how to do it without overriding whole navigation bar? All you have to do is change title of backBarButtonItem in PREVIOUS view controller. But there is another catch – you can’t just do :

self.navigationItem.backBarButtonItem.title = @"" 

becouse this won’t work. Instead of this, you need to:

UIBarButtonItem* backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;

And keep in mind – this has to be done in PREVIOUS controller, not the one you need to change your back button text. The alternative is to give up on native back button and create your own with:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"My back button" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = backButton;

Leave a Reply