I have successfully added a rightBarButtonItem to my navigationBar, but I would prefer to only have the code to do so show up once rather than once per type of ViewController. Here’s my current setup:
-->TVC
|
NVC--->TVC--->TVC--->VC
So far I’ve subclassed UITableViewController and moved my code for adding the button into my subclass. All 3 of my TableViewControllers are set to that subclass and it works perfectly.
However now I need my lone ViewController to also show the button, but I don’t know how to accomplish this without duplicating the code from my TVC subclass. Is subclassing the right answer or do I need a different approach?
Edits:
@CarlVeazey – Sure, I call it from the viewDidLoad function.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"BETA" style:UIBarButtonItemStylePlain target:self action:@selector(betaPressed)];
Do a pull-up refactor into a
UIViewControllercategory. If your project already has one, just add this code there, otherwise press cmd-N in Xcode to create a new file and choose “Objective-C Category” and enterUIViewControllerin the “Category On” field.In the interface add this method declaration:
And in the implementation add this method implementation:
Then in any
UIViewControllersubclass you can import your category header and call this method. You may also wish to add to your category an empty implementation ofbetaPressed:.BTW, ONF is the prefix I use for non-work coding so use whatever prefix already is in your project, or none at all if you’re not concerned with category name collisions.