views:

233

answers:

1

hi,

i'm creating some UILabels in my UIView, filling them with data, adding them to view and releasing them

UILabel *speed = [self scrollLabel:@"some Text" x:455.0f y:75.0f];
[scrollView addSubview:speed];

[speed release]; the method:

- (UILabel *)scrollLabel:(NSString *)text x:(float)x_ y:(float)y_ {

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x_, y_, 300.0f, 20.0f)]; [label setText:NSLocalizedString(text,@"")]; [label setFont:[UIFont fontWithName:@"Helvetica" size:14]]; [label setTextColor:[UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.9]]; [label setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]];

return label;

}

i got a button, where the user can reload the data of the uilabels. im removing the parent view of all these labels from superfiew, generating the new data and doing the method where the labels are set, again.

the problem is, the old UILabels are still existing, so my question is, whats the best way to remove this special labels?

i made a loop and removed all subviews, the problem is, i also got some other subviews in there, which i dont want to delete.

another question: is there a better way to setup font-styles for multiple Labels?

+1  A: 

I would suggest adding all the labels in a specific UIView, let's call it labelHolderView. Then every time you want to remove them, just iterate through all of its children and call removeFromSuperview for each one.

If you only want to remove specific UILabels, please provide more info as to which ones they should be.

One thing I would suggest for your code above: your - (UILabel *)scrollLabel:(NSString *)text x:(float)x_ y:(float)y_ method should return an autoreleased UILabel. So its last line should be return [label autorelease];. If you want to return a retained object, add new/copy/retain in the method's name, so that you know that the returned object is being retained every time you call it.

Consequently, you don't need to release the label after you add it to the UIView. This does not affect your specific program, but it's good to get in the habit of doing it this way so that you don't mess your retains/releases in the future.

Dimitris
thanks for your answer Dimitris. okay, ill add a own uiview to my scroll view and remove them by looping through the childrens of this view. thanks!what do you mean with "you don't need to release the label after you add it to the UIView". do you mean i dont have to release them when i return a autoreleased object in my method or generally?what will be the difference if i also add new/copy/retain to the returned value? it has a bigger releasecount then, ok, but is this an advantage in any way?
choise
First of all, your code works fine as it is. You retain a UILabel when you create it and you release it when you add it to a view. That's fine, there is no memory leak.What is bad in your code (but not wrong, just bad practice), is the fact that your method returns a retained object. In Cocoa, all the methods that return a retained object have the word *new*, *copy* or *retain* in their name, so that the developer calling them will know that he has to release the object himself. (to be continued)
Dimitris
(continued) So I was only suggesting to either autorelease the label before returning it OR just rename your method to something like `- (UILabel *)scrollLabelCopy:(NSString *)text x:(float)x_ y:(float)y_`, so that every time you call it, you are reminded of the fact that the returned object is already retained*)scrollLabelCopy:(NSString *)text x:(float)x_ y:(float)y_`, so that every time you call it, you are reminded of the fact that the returned object is already retained.Following this convention can be confusing when starting with memory management in Cocoa.
Dimitris
i'm not very familiar with "pretty coding" in coca/obj-c. thanks for this little tip.
choise