Antilope
Antilope

Reputation: 445

add/hide UITabBarItem

I have a UITabBarController and would like to add a tab, with the ability to make it disappear. I added 2 tabs with XCode. You can add a third tab by program? I know a command like:

  [self setViewControllers: [NSArray arrayWithObjects: x1, nil] animated: NO];

You can retrieve the array that uses XCode to add a third view? thanks


I can not load the view with the code. I created a view controller from the storyboard, when I try to load it from code I get a black screen, use this code:

ViewControllerA *x1 = [[ViewControllerA alloc] init];
[self setViewControllers:[NSArray arrayWithObjects: x1, nil] animated:NO];

Upvotes: 0

Views: 656

Answers (2)

Rinju  Jain
Rinju Jain

Reputation: 1694

create a custom tab bar in delegate so create a view just like a tab bar and set 1 image view on view and 3 button (1 button set hidden) add this view in tab bar controller after that when u require 3rd button enable third button

Upvotes: 0

Michael Dautermann
Michael Dautermann

Reputation: 89509

Yes, if you use [UITabViewController setViewControllers: animated:] you can add in an array containing the two previous view controllers plus the new third one.

For example, you'd probably want to do it something like this:

// assuming you've set an IBOutlet to your tab bar controller
NSArray * currentSetOfViewControllers = [appTabBarController viewControllers];
if((currentSetOfViewControllers == NULL) || [currentSetOfViewControllers count] == 0))
{
    NSLog( @"I don't see any view controllers; is my tab bar controller outlet set properly?")
} else {
    NSMutableArray newSetOfViewControllers = [[NSMutableArray alloc] initWithArray: currentSetOfViewControllers];
    if(newSetOfViewControllers)
    {
        ViewControllerA *x1 = [[ViewControllerA alloc] init];
        if(x1)
        {
            [newSetOfViewControllers addObject: x1];

            [appTabBarController setViewControllers: newSetOfViewControllers];

            // release our alloc'd mutable array only if you do not have ARC turned on
            [newSetOfViewControllers release];
        }
    }
}

You'd probably also want to give your new view controller an associated tab bar item with a title and an image. Check out [UITabBarItem initWithTitle: image: tag:].

I've linked the Apple documentation for you which I hope would help!

Upvotes: 3

Related Questions