IOS How to use viewWithTag and addSubview
Move the code [self.view addSubview:label];
inside your if
block. When your if condition is false, that means the label is
already part of of your viewcontroller's view hierarchy, so if you add
it again like in your original code it will be double retained
UILabel *label = (UILabel *)[self.view viewWithTag:100];
if (!label) {
label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)] autorelease];
label.tag = 100;
[self.view addSubview:label];
}
Comments
Post a Comment