I decided to have a custom toolbar within my navigation controller. To do so, I created this common toolbar class, which every single one of my view controller will add to its navigation item.
@implementation MainLeftToolbar
- (id)init
{
self = [super initWithFrame:CGRectMake(0, 0, 133, 44)];
if(self) {
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:5];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
// stick the buttons in the toolbar
[self setItems:buttons animated:NO];
}
return self;
}
@end
And it is set like this:
MainLeftToolbar *leftMenuToolbar = [[MainLeftToolbar alloc]init];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftMenuToolbar];
However, I have buttons and one of them is to show the camera (UIPopover). I can't seem to make the camera appear from within the UIToolbar class, therefore, I need to make it appear from the view controller that added that toolbar. I thought about using delegate methods, but this means I will have to copy the code to show the camera in every single view controller that has this custom toolbar. Any ideas?!
Ok, I can think of two options here:
Create a base class for all your viewcontrollers and implement the delegate methods there.
Create a property in you custom MainLeftToolbar that stores the presenting viewController, you set it each time you create the toolbar, that way you can write the common code in your MainLeftToolbar
class.
Personally, I prefer the first approach, this way you can write the toolbar creation code once as well in the init of the base VC, and it also follows the MVC pattern.