Question

I've build an app with around 12 view. In the top right of the NavigationBar there should be UIBarButton which has the exact same functionality for the whole app. It shows a custom button with a badge on it.

I pass the rightBarButtonItem down through TableViewControllers amd it works well. But the custom button disappears sometimes when i tap through the TabBar. Probably my solution is bad. I tried to set the custom button from the AppDelegate but i can't figure out how to reach rightBarButtonItem on every ViewController related to the TabBar.

I've tried something like this, but it always shows nil in the log.

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

    NSLog(@"item %@", self.tabController.selectedViewController.navigationItem.rightBarButtonItem);
    NSLog(@"item %@", self.tabController.selectedViewController.navigationItem.rightBarButtonItems);
    NSLog(@"item %@", self.tabController.selectedViewController.navigationController.navigationItem.rightBarButtonItems);
    //self.tabController.selectedViewController.navigationItem.rightBarButtonItem =

    [self switchTabBarImage:self.tabController.selectedIndex];
}

What is the best approach to realize somehting like a "global" rightBarButtonItem? And is the AppDelegate the right place for it?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top