tags:

views:

150

answers:

1

I have added a UILabel to my view programmatically like this:

myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 30.0f)];
myLabel.center = CGPointMake(160.0f, 120.0f);
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor whiteColor];
myLabel.font = [UIFont fontWithName:@"Helvetica" size: 18.0];
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.text = @"Hello";
[self.myView addSubview:myLabel];

To add a label to my view. What I can't seem to find out is once I'm done w/ the label (at a future point) how can I delete it from the view? [myLabel release] doesn't seem to work which I think makes sense because the view it's added to probably retained it's over reference. So what is the best practice?

+2  A: 

[myLabel removeFromSuperview];

Alfons
Does take care of the cleanup or do I then need to do [myLabel release]?
Travis
addSubview increases the retain count, so for your example: yes. It's convention to do a [myLabel release] after the addSubview. In that case you don't have to release it afterward, and the removeFromSuperview will send the final release message.HTH.
Alfons
Would it also be correct to add it to the subview, and then release it after the removeFromSuperview?
Travis
Yes, that's what I wrote, though, a bit more dense. ;-)
Alfons