refactoring similar code targeted at all view controllers's navigation bar
I'm trying to find the best way to refactor this. Right now, each of the view controllers (about 20 of them) have a function that initializes the content of the toolbar / navigation bar like so ie,
-(void)toolbarInit
and on each of the controller's viewDidLoad, you will see that开发者_JAVA百科 the function is being called ie, [self toolbarInit];
Basically what that toolbarInit does is to put a loginButton on the navigation bar's rightBarButtonItem.
Should I:
a. put the toolbar/nav bar setup in the app delegate ( is it possible to initialize each of the view controller's nav bar no matter how deep down the stack it is already?)
b. create a toolbarController or something and just put all the setup code/login code over there?
Many thanks for any other suggestions.
I'd consider one of these options:
It sounds as though you're repeatedly pushing the same or similar UIViewControllers onto the stack. Is it possible they should all derive from the same UIViewController subclass? If so, the initialization could take place in a superclass shared by all of the 20 or so items in the stack.
If the view controllers have different superclasses, consider defining a category on UIViewController that handles the instantiation and addition of your buttons. Then you need only import the header and call the same
[self toolbarInit]
inviewDidLoad
.Same as above, except instead of using a category, create a standalone class for handling setup like this. The logic in
toolbarInit
could be stuck in a class method likesetupNavigationItem:
.
精彩评论