Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating UIButton using helper method
    primarykey
    data
    text
    <p>I have a subclass of <code>UITableView</code> and in it I want to generate number of labels that share the same properties (font, textColor, backgroundColor, etc.).</p> <p>I decided the easiest way to achieve this would be to create a helper method which creates the label with some common properties set:</p> <pre><code>- (UILabel *)defaultLabelWithFrame:(CGRect)frame { UILabel *label = [[UILabel alloc] initWithFrame:frame]; label.font = [UIFont fontWithName:@"Helvetica" size:14]; label.textColor = [UIColor colorWithWhite:128.0/255.0 alpha:1.0]; label.backgroundColor = [UIColor clearColor]; return label; } </code></pre> <p>I use the method like this:</p> <pre><code>UILabel *someLabel = [self defaultLabelWithFrame:CGRectMake(0,0,100,100)]; [self addSubview:someLabel]; [someLabel release]; </code></pre> <p>My concern here is that <strong>when creating the label in the method it is retained, but when I then assign it to someLabel, it is retained again and I have no way of releasing the memory when created in the method</strong>.</p> <p><strong>What would be best the best approach here?</strong></p> <p>I fee like I have two options:</p> <ol> <li> Create a subclass of UILabel for the default label type. </li> <li> Create an NSMutableArray called <b>defaultLabels</b> and store the labels in this: </li></ol> <pre><code>- (UILabel *)defaultLabelWithFrame:(CGRect)frame { UILabel *label = [[UILabel alloc] initWithFrame:frame]; label.font = [UIFont fontWithName:@"Helvetica" size:14]; label.textColor = [UIColor colorWithWhite:128.0/255.0 alpha:1.0]; label.backgroundColor = [UIColor clearColor]; [defaultLabels addObject:label]; [labels release]; //I can release here return [defaultLabels lastObject]; //I can release defaultLabels when done } </code></pre> <p>I appreciate your thoughts. Cheers.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload