2

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?

rockstarberlin
  • 1,853
  • 1
  • 18
  • 31

1 Answers1

3

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
Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • i just see it's not working together with my TabBar. When one ViewController is shown the second time the custom rightButtonBarItem disappears. After tapping through all taps only the last one shows the rightButtonBarItem. Any idea? – rockstarberlin Feb 20 '13 at 02:48
  • do you have any idea how this could work with custom buttons like this: self.sharedRightButton = [[UIBarButtonItem alloc] initWithCustomView:btn]; ? – rockstarberlin Feb 20 '13 at 04:20
  • it seems it is way more complicated then i thought. i´ve removed the lines in the AppDelegate and have subclassed the TabBarController. It worked well with the standard button but not with the custom button. The custom button appeared only the first time the viewcontroller was shown. I´ll follow your suggestion and change my code. Thank you! – rockstarberlin Feb 20 '13 at 17:12