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?
You want a subclass of
UIViewController
instead of a catagory.For example:
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:
From this.... to this...
If you're using storyboards, you'll want to change the superclass from within the Identity Inspector...
If you want to override
backBarButtonItem
, overridebackBarButtonItem
. There is one and only one method calledbackBarButtonItem
. ObjC methods are uniquely determined by their name.You'd do it like so:
I'm not saying it's a good idea, but that's how you'd do it.