Reputation: 1623
I have an app with a navcontroller created in the appdel. Each vc pushed in has a block of code in the viewdidload that sets up the toolbar. The toolbar is always the same. Is there a way for me to just create this code once - and not put it in every vc?
[self.navigationItem setHidesBackButton:YES];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(backClicked)];
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *storyBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Sto" style:UIBarButtonItemStyleBordered target:self action:@selector(toolbarControl:)];
storyBtnItem.tag = 1;
UIBarButtonItem *renderBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Ren" style:UIBarButtonItemStyleBordered target:self action:@selector(toolbarControl:)];
renderBtnItem.tag = 2;
UIBarButtonItem *amenBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Ame" style:UIBarButtonItemStyleBordered target:self action:@selector(toolbarControl:)];
amenBtnItem.tag = 3;
UIBarButtonItem *availBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Availability" style:UIBarButtonItemStyleBordered target:self action:@selector(toolbarControl:)];
availBtnItem.tag = 4;
UIBarButtonItem *eopBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Eq" style:UIBarButtonItemStyleBordered target:self action:@selector(toolbarControl:)];
eopBtnItem.tag = 5;
UIBarButtonItem *stkBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"St" style:UIBarButtonItemStyleBordered target:self action:@selector(toolbarControl:)];
stkBtnItem.tag = 6;
UIBarButtonItem *movBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Fi" style:UIBarButtonItemStyleBordered target:self action:@selector(toolbarControl:)];
movBtnItem.tag = 7;
NSArray *items = [NSArray arrayWithObjects:flexibleSpaceLeft, stoBtnItem, reBtnItem, ameBtnItem, avaBtnItem, eBtnItem, stBtnItem, mvBtnItem, nil];
[self setToolbarItems:items];
[self.navigationController.toolbar setTintColor:[UIColor colorWithRed:79.0/255.0 green:145.0/255.0 blue:205.0/255.0 alpha:1.0]];
Upvotes: 1
Views: 375
Reputation: 15376
Just do vc.toolbarItems = self.toolbarItems
(where vc
is the view controller to be pushed) in the method where you are pushing the next view controller.
eg:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *vc = [UIViewController new];
vc.toolbarItems = self.toolbarItems
[self.navigationController pushViewController:vc animated:YES];
[vc release]; // if not using ARC
}
Also, you don't need to do this in the -viewDidLoad
method, setting the navigation items and toolbar items does not require the view to be loaded and can thus be done in your init
or awakeFromNib
method. If you do it in -viewDidLoad
you are potentially setting the items multiple times.
Upvotes: 2
Reputation: 9544
You could make an abstract sub-class for your viewControllers that has this function in its ViewDidLoad. Then you just adjust it in one place.
For example, make a view controller class called myMasterViewController. All you have to set up in it is the viewDidLoadMethod. Then in your other controllers make them inherit from myMasterViewController instead of UIViewController. Make sure you have [super viewDidLoad];
in your other VC's.
Upvotes: 0
Reputation: 5399
The way i usually handle those sort of things is to create a base view controller, and the code to setup the toolbar will be in the base view controller, and all other view controllers inherit from this base controller.
Upvotes: 0