Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bar Button Item in only one of the tab bar controller navigation bar

I have a tab bar controller with 4 views controller, and have this tab bar controller in a navigation controller.

I want to display a UIBarButtonItem for just one specific view controller of the tab bar controller.

I tried to use the following

if (tabBarController.selectedViewController == customTourViewController)
    {
        [tabBarController.navigationItem setRightBarButtonItem:done];
    }

But the button doesn't show up.

If I put every view controller in a navigation controller, then the button shows up for only that view, but I end up having 2 navigation bars.

Is there any way I can implement the first solution? Thanks.

like image 820
ratsimihah Avatar asked Jul 23 '12 22:07

ratsimihah


People also ask

How do I add a tab bar to my navigation controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

What is a tab bar view controller?

A tab bar controller is a powerful UI component for iOS apps. It's a container view, and you use it to group view controllers together. They give your app's user access to the most important screens of your app.

How do I hide the navigation bar button?

Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.


2 Answers

In my individual view controllers for the individual tabs, I have the following in the one that needs the button:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                    style:UIBarButtonSystemItemDone target:nil action:nil];
    self.tabBarController.navigationItem.rightBarButtonItem = rightButton;
}

And in the view controllers that don't need the button, I have:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.tabBarController.navigationItem.rightBarButtonItem = nil;
}

So, if it's not working for you, I'm not sure if it's your reference to tabBarController without the self designation (if I omit the self I get a compiler error). And where is this code because if it's in your tabBarController subclass, then you want self.navigationItem.rightBarButtonItem, right? Do you have your own ivar defined for that variable name? Or are you sure that done is defined properly (i.e. not nil)? Or are you sure this code is being called at all (perhaps set a breakpoint or insert a NSLog and make sure this code is being reached)?

like image 88
Rob Avatar answered Sep 28 '22 10:09

Rob


Alternatively you can implement viewWillDisappear in the same view where you need the button.

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];        
    self.tabBarController.navigationItem.rightBarButtonItem = nil;
}
like image 26
Johnny Z Avatar answered Sep 28 '22 09:09

Johnny Z