Reputation: 25244
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. I'm 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 don't want to delete.
Another question: Is there a better way to setup font-styles for multiple Labels?
Upvotes: 1
Views: 1057
Reputation: 13670
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.
Upvotes: 1