0

I want to write a category on UINavigationItem to make changes to barBackButtonItem across my entire app.

From what I have been told in the comments here ( Change BackBarButtonItem for All UIViewControllers? ), I should "override backBarButtonItem in it, then your method will be called whenever their back bar button item is called for." - but how do I know what method to override? I have looked at the UINavigationItem documentation, and there are multiple methods used for initializing a backBarButtonItem. How do I determine which method I should override in my category?

Community
  • 1
  • 1
Chris
  • 5,485
  • 15
  • 68
  • 130
  • You cannot override methods in a category, do you mean a subclass? – Hamish Jan 14 '16 at 16:14
  • Ok that is what I thought but the person on the other post seemed confident I could override `backBarButtonItem` in a Category ... Please look here if you can: http://stackoverflow.com/q/34773263/300129 – Chris Jan 14 '16 at 16:40
  • no, he's wrong. Attempting to override a method in a category will flag a warning in Xcode and won't give you desired results. What you want to do is _subclass_ `UIViewController`, and in the `-viewDidLoad` set your `self.navigationItem.backBarButtonItem.title` to what you want. – Hamish Jan 14 '16 at 16:46
  • Ok so the end point is, I have to go through each View Controller and change the title of each backBarButtonItem, correct? – Chris Jan 14 '16 at 16:54
  • No, you only need to change it in the subclass. That change will be reflected in all your view controllers provided you change their class to the subclass you made. – Hamish Jan 14 '16 at 16:56

2 Answers2

0

If you want to override backBarButtonItem, override backBarButtonItem. There is one and only one method called backBarButtonItem. ObjC methods are uniquely determined by their name.

You'd do it like so:

@implementation UINavigationItem (MyCategory)

- (UIBarButtonItem *)backBarButtonItem
{
    return [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil]
}

@end

I'm not saying it's a good idea, but that's how you'd do it.

Simon
  • 25,468
  • 44
  • 152
  • 266
-1

You want a subclass of UIViewController instead of a catagory.

For example:

@interface CustomViewController : UIViewController

@end

@implementation CustomViewController

-(void) viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.backBarButtonItem.title = @"";
}

@end

Now you just need to use the CustomViewController class for your view controllers, and they will all have the changes applied to them.

If you're doing this programatically, then you'll just want to change the superclass of the view controllers:

enter image description here

From this.... to this...

enter image description here

If you're using storyboards, you'll want to change the superclass from within the Identity Inspector...

enter image description here

Hamish
  • 78,605
  • 19
  • 187
  • 280