views:

173

answers:

2

I have an app that uses various view controllers as tabs. I'm in the process of saving the custom tab order when the app shuts down. I'm trying to find a generic identifier for my various view controllers without having to add an attribute to each controller (which my backup plan).

I would think, similar to UILabels, that maybe view controllers would support tagging or something similar? I'm having trouble finding in documentation so is there a way to tag or otherwise uniquely identify each view controllers in my UITabBarController?

A: 

I'm pretty sure you'll have to just go with your backup plan. There are advantages to that, anyway, because it gives you more control over the whole process and how you'd like to save and differentiate between your tabs.

It shouldn't be much work to quickly subclass UITabBarController and add a string/int to identify the items.

mjdth
+3  A: 

How has nobody suggested the correct answer so far? When creating your tab bar items, use the tag property of the tab. It's easily accessible in Interface Builder as well.

- (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.

Ed Marty
Boy, that was obvious -- how indeed? Tip of the hat to you. I'd add that it might be worthwhile to set up an enum to keep track of the tags.
Danilo Campos
Ed. My savior! Thank you so much. No one had answered this (I thought) seemingly simple question. I am actually pushing the view controllers onto the tab controller pragmatically (most tabs are actually custom view controller inside a nav controller then pushed onto the tab controller). The enum suggestion is clever as well.Thank you all!
Travis