0

I created a tabBar app in Interface Builder - and named all my tabs and tab-items there. Can I now programmatically find out or retrieve the names of my tabs during run-time? When I do an NSLog of the viewControllers array, all I get is:
( "", "", "", "", "" )

That's obviously no good - I need to get more specific info.

Also, I noticed you can't set a tag, title or label in IB to any of the view-controllers in your tabs.

So how can you identify through code what you have sitting in your various tabs? Or do you have to ditch Interface Builder and do everything by code?

sirab333
  • 3,662
  • 8
  • 41
  • 54

2 Answers2

1

The tab bar item within each view controller contains the title.

for (UIViewController *viewController in tabBarController.viewControllers) {
    NSLog(@"%@", viewController.tabBarItem.title);
}

Having said that, I think the best way to refer to specific tabs is to have explicit IBOutlets from the app delegate to each view controller. Since there's not going to be more than a handful, this shouldn't be too much extra code.

Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
  • I just copy-pasted your code and I'm getting this error: "use of undeclared identifier 'var' " – sirab333 Nov 29 '11 at 12:49
  • In terms of specifying IBOutlets from the app-delegate to each view controller - do you mean to each tab? So is the code something like this: "IBOutlet UITabBarItem *tab1, *tab2, *tab3;" etc., and then drag arrows from File's Owner to each tab? – sirab333 Nov 29 '11 at 13:12
  • `var` should've been `UIViewController *` (sorry, wrong language on my mind). You want to make the outlets to the view controllers, so declare them as `@property (nonatomic, retain) IBOutlet UIViewController *`. – Daniel Dickison Nov 29 '11 at 17:38
1
for(id btn in TabObject.tabBar.items)
    {
        if([btn isKindOfClass:[UITabBarItem class]])
        {
            UITabBarItem *sender=btn;
            NSLog(@"%@",sender.title);
        }
    }
Srinivas
  • 983
  • 9
  • 21