Utilizing the tag property

Using Objective-C, can someone post a code sample showing how one would programmatically assign an integer to the tag property of a control (let's say a label), and then spit out the text of the label into NSLog by calling its tag?

MacBook w/ 2GB RAM, 100GB HD, Mac OS X (10.5.6), iPhone 16GB

Posted on May 25, 2009 9:46 PM

Reply
2 replies

May 25, 2009 11:52 PM in response to Brian Reading

Brian Reading wrote:
Using Objective-C, can someone post a code sample showing how one would programmatically assign an integer to the tag property of a control (let's say a label), and then spit out the text of the label into NSLog by calling its tag?


- (void)viewDidLoad {
[super viewDidLoad];
// create a column of 6 labels with tag nos. 100-105
CGRect frame = CGRectMake(20, 0, 120, 37);
for (int i=0, tag=100; i < 6; i++, tag++) {
frame.origin.y = frame.origin.y + 50;
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = [NSString stringWithFormat:@"Label No. %d", i];
label.textAlignment = UITextAlignmentCenter;
label.tag = tag;
[self.view addSubview:label];
[label release];
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// get the text for label 103
UILabel *label = (UILabel*)[self.view viewWithTag:103];
NSLog(@"Text for label 103: %@", label.text);
}

- Ray

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Utilizing the tag property

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.