2

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

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
pa12
  • 1,493
  • 4
  • 19
  • 40

3 Answers3

2

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.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • What I am trying to do is I am having a tabbar and want to add different views for each item like what we have in tabbar controller. I did not figure out how to assign a viewcontrollor for tabbarItem – pa12 Jun 30 '11 at 15:49
1

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.

PengOne
  • 48,188
  • 17
  • 130
  • 149
1

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.

Deeps
  • 4,399
  • 1
  • 26
  • 27