Note, my original answer below, which entails letting the tab bar controller create a single tab bar button that is used by all of the navigation bars for all of the controllers included in the tab bar controller only works if you're creating a simple UIBarButtonItem
. Better (and critical if you're using a custom UIBarButtonItem
with images and the like), is to create a new bar button item subclass that has the behavior you want, and then add that to all of your controllers' navigation bars. My answer to your follow up question at Custom rightBarButtonItem disappearing for an example implementation.
I'll keep my original answer below for historical purposes.
Personally, I don't think the app delegate is the right place (though in practice, it might not matter). I would personally would subclass the UITabBarController
and put it there, and then have the subviews grab it from there. For example, the interface for the subclassed UITabBarController
might look like:
// MyTabBarController.h
@interface MyTabBarController : UITabBarController
@property (nonatomic, strong) UIBarButtonItem *sharedRightButton;
@end
With an implementation that creates the button (and has the method that is invoked if that button is pressed):
// MyTabBarController.m
@implementation MyTabBarController
- (void)viewDidLoad
{
[super viewDidLoad];
// you'll do your own, fancy button instead of this simple bordered button
self.sharedRightButton = [[UIBarButtonItem alloc] initWithTitle:@"Test"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(clickedTest:)];
}
// you'll have your own action method here
- (void)clickedTest:(id)sender
{
NSLog(@"%s", __FUNCTION__);
}
@end
Finally, each of the view controllers that are presented by the tab bar controller could do something like:
@implementation FirstTabViewController
- (void)viewDidLoad
{
[super viewDidLoad];
MyTabBarController *tabBarController = (MyTabBarController *)self.tabBarController;
self.navigationItem.rightBarButtonItem = tabBarController.sharedRightButton;
}
@end