Domanda

//Update provided I have a storyboarded UIViewController application. I have other XIB (inherited from UIView having only a label on it) like below

The following are contents Main.storyboard (MyController is an interface extending UIViewController)

> ViewController         (UIViewController)
    > MyHolderView       (UIView)

The following are contents of MySquare.xib (MySquare is an interface extending UIView)

> MySquare               (UIView)
    > MyLabel            (UILabel)  (Having Default value LABEL, entered in attribute inspector)

Now I have to make 3 UIViews of instances MySquare and add it to MyHolderView

I tried to assign new label to these 3 UIView's labels' text. But I am not able to see the new labels but only the default label LABEL is coming.

MySquare *square=[[MySquare alloc]init]; 
//square.myLabel.text = @"TRY";
[square.myLabel setText:[[NSString alloc]initWithFormat:@"%d",(myVar)]];

Please help.

Update I have overrode my MySquare's init method like this. Still no luck. I am calling the below method from UIViewController where I init my MySquare views. Calling from UIViewController:

        MySquare *square=[[MySquare alloc]initWithFrame:CGRectMake(20,20,50,50) string:[[NSString alloc] initWithFormat:@"%d",myVar]];

Implementation of the overridden init function

- (id)initWithFrame:(CGRect)frame string:(NSString *)str;
{
    self = [super initWithFrame:frame];
    if (self) {
        self.myLabel.text=@"A";
        [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil] objectAtIndex:0]];
        [self.myLabel setText:[[NSString alloc]initWithString:str]];
 }
return self;

}

È stato utile?

Soluzione

You need to understand the difference between a class and an instance. MySquare is a class, but you need a reference to the actual instance of MySquare in your interface. Thus this code is pointless:

MySquare *square=[[MySquare alloc]init]; 
[square.myLabel setText:[[NSString alloc]initWithFormat:@"%d",(myVar)]];

It is working perfectly, but the problem is that this MySquare instance is not the MySquare instance that is in your interface. (It is just a separate MySquare instance that you created, floating loose in your code.) Thus you cannot see anything happening.

Now let's consider this code:

    [self addSubview:[[[NSBundle mainBundle] 
         loadNibNamed:@"View" owner:self options:nil] objectAtIndex:0]];
    [self.myLabel setText:[[NSString alloc]initWithString:str]];

Here, you did fetch a MySquare instance from the nib and put it in your interface. Good. But you did not keep any reference to it, so you have no (easy) way to talk to it! In particular, self.myLabel is not the same as the MySquare instance's myLabel.

You left out a step! You need a reference to the MySquare instance, like this:

    MySquare* square = [[[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil] objectAtIndex:0]];
    [self addSubview:square];
    [square.myLabel setText:@"Look, it is working!"];

Even that is not enough, if you want to talk to square.myLabel in the future. You will need to keep a reference to square (or to square.myLabel) as an instance variable.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top