Add the same UIBarButtonItem to several UIViewCont

2019-08-01 03:53发布

问题:

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.

回答1:

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