Reputation: 9437
I know it is possible to add a custom button in a navigation item using this:
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self
action:@selector(showInfo:)
forControlEvents:UIControlEventTouchUpInside];
// Add the info button to the navigation bar
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[self.navigationItem setRightBarButtonItem:barButtonItem
animated:YES];
[barButtonItem release];
But is there a way of setting this button in every subview pushed to the navigation stack without having to replicate this code?
Thanks!
Upvotes: 1
Views: 1230
Reputation: 45170
When pushing a "next" view controller you can add this code:
nextVC.navigationItem.rightBarButtonItem = [self.navigationItem.rightBarButtonItem copy];
But you have to overwrite the selector (if you want your button to send data to your nextViewController object):
[nextVC.navigationItem.rightBarButtonItem setTarget:nextVC ...];
Upvotes: 0
Reputation: 14549
You could also subclass UIViewController, then override:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];
// Add the info button to the navigation bar
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[self.navigationItem setRightBarButtonItem:barButtonItem animated:YES];
[barButtonItem release];
}
return self;
}
then when you make a new VC, subclass this class instead of UIViewController
Upvotes: 4