Reputation: 53
I'm using interface builder's tag feature to access some UILabels I'm instantiating in a xib file. Since this a UITextViewCell I want to avoid superfluous method calls, but I want to do it right too. Thus when I do:
UILabel *label = (UILabel *)[cell viewWithTag:1];
I'm wondering if I should wrap it up like so:
if([[cell viewWithTag:1] isKindOfClass [UITableViewCell class]]) {
UILabel *label = (UILabel *)[cell viewWithTag:1];
}
Any discussion on this would be appreciated.
Thanks
Upvotes: 3
Views: 5341
Reputation: 11
You set tags in interface builder using command-1 in the attribute editor and look for 'tag' underneath the background setting.
Upvotes: 1
Reputation: 4651
Yes I find the enum method works well, or just doing #define foo 1, #define bar 2, etc.
Upvotes: 1
Reputation: 8823
Like August, I'd suggest making your tag numbers unique within that branch of your view hierarchy.
I'd also suggest that you set up an enum
to enumerate your possible tag values so that your viewWithTag:
method becomes more readable.
Upvotes: 6
Reputation: 12177
Unless you have a lot of different objects of different classes that have subviews tagged '1' then this isn't necessary.
If you DO have a bunch of different objects (cells, say) that have different classes but all have subviews with a 1 tag, I'd reconsider your tagging scheme. Perhaps 101, 201, & 301, etc.
Upvotes: 3