1

I want to add the same UIBarButtonItem to several UIViewControllers. I've done this previously by creating a parent UIViewController that manages to add the UIBarButtonItems in viewDidLoad. But now I have many view controllers that inherit from different view controllers, so if I follow the same strategy I should create a UIViewController subclass for each one of the different view controllers.

What I am actually doing is to configure the navigation bar buttons in my BaseViewController, the parent controller of all of the rest, depending on the flags I set when creating the child view controllers. With this solution the code only relies on one view controller, but I would like if there are more elegant options to achieve this.

amb
  • 4,798
  • 6
  • 41
  • 68

1 Answers1

7

One of solutions is to use objective-c category. In this case, you will create common method for all UIViewControllers, that will create and configure UIBarButton

Mykola Denysyuk
  • 1,935
  • 1
  • 15
  • 15
  • Do you mean an ``UIViewController`` category? – amb Feb 25 '14 at 18:44
  • 1
    @amb sure, it should be `UIViewController's` category. also, it can be category for any other class that is base for your group of view controllers, but if you have class like that, I think it will be more properly to implement separated method in that base class. – Mykola Denysyuk Feb 26 '14 at 09:00
  • I've just implemented this as a category of my ``BaseViewController``. Works perfectly and is the best solution I've seen so far. I cannot implement this as a method in my base VC because it needs to instantiate another VC that inherits from the base one. – amb Feb 26 '14 at 09:40