开发者

Passing a NSString between classes

So I'm kind of confused on passing a NSString to a class. I have a legend class that draws a little box, and has a label on what the box is. My box is essentially a UIView object where I change the background color. In my init method, I initialize the box, and then from my other class where I create an instance of this class, I set the background color. However, in the case of the NSString, I don't know how to alloc/init, and set its text from another class. Here's my code for the legend.

- (id)sharedInit {
    self.backgroundColor = [UIColor clearColor];

    self.LegendBox = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height / 2 - LEGEND_SIZE / 2, LEGEND_SIZE, LEGEND_SIZE)]; // hugs LHS, centered vertically

    self.LegendLabel = [[NSString alloc] init];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 0, self.bounds.size.width - 25, self.bounds.size.height)]; 
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
    label.text = self.LegendLabel;
    label.font = [UIFont fontWithName:@"Arial" size:14.0];
    label.numberOfLines = 5;
    label.lineBreakMode = UILineBreakModeWordWrap;

    [self addSubview:label];
    [self addSubview:_legendBox];

    return self;
}

Then when I try to create an instance of the legend class in my other class:

PieChartLabelView *labelView1 = [[PieChartLabelView alloc] initWithFrame:CGRectMake(0, 0, 140, 80)];
labelView1.LegendBox.backgroundColor = BLUE_COLOR;
NSString *str1 = [[NSString alloc] initWithFormat:@"as;flkasjdf;laksjdf"];
开发者_开发百科labelView1.LegendLabel = str1;

[self addSubview:labelView1];
 [str1 release];
[labelView1 release];

I do not get any text for my label.

A second question I have in trying to better understand self/ivars/setters/getters, is in my legend class, why can I just use

[self addSubview:_legendLabel];

versus self.LegendLabel? I still don't understand that fully. Thanks.


Second Part

To answer your second question first, doing either

[self addSubview:_legendLabel];

or

[self addSubview:self.legendLabel];

should be the same if you are not doing any lazy instantiation. The getter method, most times, retrieves the value so you should be good adopting the first approach. But if you are doing any lazy instantiation by defining your own getter method where you do something like this,

- (UILabel *)legendLabel {
    if ( !_legendLabel ) {
        // Instantiate _legendLabel
    }

    return _legendLabel;
}

then it will have a different meaning if you don't do self.legendLabel.

First Part

This is where your second question adds to a bit of confusion, you seem to refer LegendLabel as an NSString variable while you try to add it as a subview in your second question.

However, you seem to be expecting this –

labelView1.LegendLabel = str1;

to change the label text because you do this in the sharedInit method –

label.text = self.LegendLabel;

That will not affect the label text for two reasons,

  1. If both the label's text property and LegendLabel were to point to the same object, then you would need the object to be mutable so that the changes are reflected on the label. If you reassign LegendLabel then both of them will point to two different objects.
  2. Most times NSString properties are declared as copy properties to guard against mutability. So you can't change the label's text even if LegendLabel were mutable.

So that means you will have to make the label an instance variable or a property so that you can directly access the label and update it's text property or rewrite LegendLabel's setter method to update the label.

However I think declaring label as a property is the approach to take and LegendLabel's naming is a tad confusing. It would be much nicer if it were a label.


When you create a NSString you should do it directly like this:

NSString *str1 = @"ñalskdfjsdañlkfj";

If you want to use a format you do it like this:

NSString *str2 = [NSString stringWithFormat:@"This is string %d", 2];

If you want to use it as a property in a class you should declare it like this:

@property (copy) NSString *str3;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜