0
votes

I have a tab bar controller as my root view with 5 navigation controllers (one for each tab). The navigation bar in each tab will have a button that has the same functionality across all tabs. Is there an easier way to add this button (and respond to selection) than copy/pasting it into each navigation controller?

EDIT: In my custom navigation controller's child view controller, I have:

- (void)viewDidLoad
{
    [super viewDidLoad];
    ...
    CustomNavigationController *navController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeNavigationController"];
    [navController addNotificationsButton:YES searchButton:NO];
}

and in the custom navigation controller I have:

-(void)addNotificationsButton:(BOOL)notifications searchButton:(BOOL)search {   //Choose which bar button items to add

    NSMutableArray *barItems = [[NSMutableArray alloc]init];

    if (notifications) {
        //Create button and add to array
        UIImage *notificationsImage = [UIImage imageNamed:@"first"];
        UIBarButtonItem *notificationsButton = [[UIBarButtonItem alloc]initWithImage:notificationsImage landscapeImagePhone:notificationsImage style:UIBarButtonItemStylePlain target:self action:@selector(notificationsTap:)];

        [barItems addObject:notificationsButton];
    }

    if (search) {
        //Create button and add to array
        UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchTap:)];
        [barItems addObject:searchButton];
    }

    [self.navigationItem setRightBarButtonItems:barItems];
}

But I just get an empty navigation bar when the child view controller loads. Any ideas?

EDIT 2: I just had to add an argument for the view controller who's navigation bar I wanted to add buttons to. Here is the final implementation...

for CustomViewController.m:

- (void)viewDidLoad
    {
        [super viewDidLoad];
        ...
        CustomNavigationController *navController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeNavigationController"];
          [navController addNotificationsButton:YES searchButton:NO forViewController:self];

}

For CustomNavigationController.m

-(void)addNotificationsButton:(BOOL)notifications searchButton:(BOOL)search forViewController:(UIViewController*)vc {   //Choose which bar button items to add


    NSMutableArray *barItems = [[NSMutableArray alloc]init];

    if (notifications) {
        //Create button and add to array
        UIImage *notificationsImage = [UIImage imageNamed:@"first"];
        UIBarButtonItem *notificationsButton = [[UIBarButtonItem alloc]initWithImage:notificationsImage landscapeImagePhone:notificationsImage style:UIBarButtonItemStylePlain target:self action:@selector(notificationsTap:)];

        [barItems addObject:notificationsButton];
    }

    if (search) {
        //Create button and add to array
        UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchTap:)];
        [barItems addObject:searchButton];
    }


      vc.navigationItem.rightBarButtonItems = barItems;

}
2
Note: you have to add @property (strong, nonatomic) CustomNavigationController *navController; to your customViewController or else ARC will dealloc your CustomNavigationController, causing EXC_BAD_ACCESS when you tap a buttonNick Yap

2 Answers

0
votes

You need to create a subclass of UINavigationController, configure it as you want (so with your button), and set that as class of your 5 Navigation Controller.

Obviously i can't write here all your apps, but the important is that you have a way.

If you are a developer at the first times, i encourage you to study about subclassing etc.

0
votes

Subclass navigation bar and add button as part of subclass. Now you can use 5 instances of the subclass instead of the 5 base navigation bars and cut out the repeated work of adding the buttons.