I have my ViewController.h/m and another class Keyboard.h/m.
In my ViewController.h I have an UILabel:
@interface ViewController : UIViewController{
UILabel *label;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
and my ViewController.m looks so
#import "ViewController.h"
@synthesize label;
...
Now I want to change the label from Keyboard.m.
I have tried something like this:
#import "ViewController.h"
...
ViewController *vc;
vc.label.text = @"text";
it compiles without any errors but the label doesn’t change
It’s very error prone that you’re doing here.
declares a pointer, but this won’t be initialized; so when you’re accessing its property vc.label.text, objc_messageSend() will be passed a bogus pointer, so it can potentially crash! (you’re lucky if id didn’t do so.)
Anyways: if you have done it well, like
ViewController *vc = [[ViewController alloc] init];creating a new instance wouldn’t have affected the other instance. You have to store the pointer to your instance somewhere, e. g. set a@property (retain) ViewContrller *vc;to your application’s app delegate object, and access it through that property like this:that way it should work.
Hope it helps.