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.