2

The 'Tag' property of a UITabBarItem sounds like a useful thing, but in practice, how does one use it? According to the documentation:

- (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag Parameters

title

The item's title. If nil, a title is not displayed.

image

The item's image. If nil, an image is not displayed.

The images displayed on the tab bar are derived from this image. If this image is too large to fit on the tab bar, it is scaled to fit. The size of an tab bar image is typically 30 x 30 points. The alpha values in the source image are used to create the unselected and selected images—opaque values are ignored.

tag

The receiver's tag, an integer that you can use to identify bar item objects in your application.

But the only code I have found to retrieve a UITabBarItem by its tag is something similar to the following:

for (UIViewController *viewController in stTabBarController.viewControllers) {
    if (viewController.tabBarItem.tag == MyTabBarItemTag) {
        // do stuff
    }
}

I can make this as a category function, but it seems like I'm missing something. Surely something like this is already implemented? Or am I just missing the point of the tag attribute?

death_au
  • 1,282
  • 2
  • 20
  • 41
  • if you put a view controller inside a UITabBarController, you can access the UITabBarItem inside the view controller with self.tabBarItem. – meim Nov 26 '12 at 06:48
  • 1
    The code you posted to iterate through the tab items is the correct way. There is no standard method for this like the `UIView viewWithTag:` method. – rmaddy Nov 26 '12 at 06:55
  • 1
    @Shadowfiend - I'm aware of that, but what if you wish to get information from another tab? It's normal for people to use the `objectAtIndex:` method, but then you have to fix all your code if you change the tab order. – death_au Nov 26 '12 at 22:33
  • we can define an enum to store the order of the tabs and call [self.tabBarController.tabBar.items objectAtIndex:x] to get the specific tab. – meim Nov 26 '12 at 23:23
  • 2
    @Shadowfiend - Yeah, you can do that. So, going back to the question, the "tag" attribute is absolutely pointless? – death_au Nov 27 '12 at 01:36
  • [self.tabBarItem.superview viewWithTag:xx]? – meim Nov 27 '12 at 05:49

0 Answers0