pa12
pa12

Reputation: 1503

uitabbar and uitabbaritem

I am trying to add UITabBarItems to a UITabBar not to a tabbar controller. Here is what I tried to do. It is always crashing when I am calling setItems. Can any please point out whats wrong.

My_Accounts *my_AccountsVC = [[My_Accounts alloc] init];
Payments *paymentsVC = [[Payments alloc] init];
Transfer *transferVC = [[Transfer alloc] init];
NSArray *VCArray = [[NSArray alloc] initWithObjects:my_AccountsVC,paymentsVC,transferVC, nil];
[self.tabbar setItems:VCArray];

Thanks

Upvotes: 2

Views: 3281

Answers (3)

Deeps
Deeps

Reputation: 4577

Code seems wrong. I guess

[self.tabbar setItems:VCArray];

Above line should have parameter of Array of UITabBarItems. You passed items of UIViewController I guess. You should Create UITabbarItems and pass array of that in setItems method.

You should do something like below:

    UITabBarItem *tabOne = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0];
    UITabBarItem *tabTwo = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];

    NSArray *arrTabbarItems = [NSArray arrayWithObjects:tabOne,tabTwo, nil];

    [tabbar setItems:arrTabbarItems];

I am not sure what it will do as I am always using UITabBarController. Hope this help.

Upvotes: 1

PengOne
PengOne

Reputation: 48398

I believe you are misunderstanding how a UITabBarController works (documentation link). You must add the UIViewControllers to the UITabBarController using the viewControllers property.

The last line you have should read:

[tabBarController setViewControllers:VCArray];

The tabBar property of the UITabBarController is read-only. You cannot set that.

If you have a UITabBar (documentation link) without a UITabBarViewController, then you will need to use the method:

- (void)setItems:(NSArray *)items animated:(BOOL)animated

However, these items are not UIViewControllers! They are instances of UITabBarItem (documentation link). You may set these all at once by putting them into an array, or you can set them per view controller. There are several system items you may use (More, Favorites, etc) or you may use – initWithTitle:image:tag: to create a custom item.

Upvotes: 1

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

If you look at items, it takes an array of UITabBarItems and not UIViewController subclasses which you seem to be passing.

You will have to keep track of the view controllers elsewhere and pass an array of UITabBarItems and handle the view controllers in the UITabBar's delegate.

Or much better, use UITabBarController.

Upvotes: 2

Related Questions