Christian Gossain
Christian Gossain

Reputation: 5972

How can I update my view controller's UILabel through another class?

I'm my class i've added a an instance of my view controller, created a property and then synthesized it in my implementation file. I am trying to update the UIlabel in the view controller like this,

NSString *currentChar = [[NSString alloc] initWithFormat:@"%c", ch];
viewController.outputLabel.text = currentChar;
[currentChar release];

My problem is that everything builds without any errors or warnings but the label just doesn't get updated, what am I doing wrong. I'd really appreciate some help on this one.

Upvotes: 1

Views: 2035

Answers (3)

Daddy
Daddy

Reputation: 9035

Are you sure you're referencing the existing viewController and you didn't instantiate a new one? Your property is not declared as copy, correct?

textProcessor.h / .m

@interface textProcessor : NSObject {

MainViewController *mainView;

}

@property (retain) MainViewController *mainView;

@end

@implementation textProcessor;
@synthesize mainView;

MainViewController.h / .m

@interface MainViewController : UIViewController {

UILabel *myLabel;

}
@property (retain) UILabel myLabel;

@end

@implementation MainViewController
@synthesize myLabel;

When you are initializing your textProcessor class, and you set the value for "mainView" like

-(void)viewDidLoad {
    [super viewDidLoad];
    textProcessor *proc = [[textProcessor alloc] init];
    proc.mainView = self;
    //note that you are not doing this:
    //MainViewController *mainView = [[MainViewController alloc] init];
    //proc.mainView = mainView;
    //that was creating a new instance variable instead of using self, the existing one
    [textProcessor release];
}

Upvotes: 2

ennuikiller
ennuikiller

Reputation: 46985

Have you tried calling the setNeedsDisplay method on the view? Also you may want to try using the setText method instead of assigning directly to the property.

Upvotes: 0

Aaron
Aaron

Reputation: 1

Have you created your label in IB? If you are using IB you have to create an IBOutlet for your UILabel. You then make a connection between the UILabel in IB to your IBOutlet in your class.

Upvotes: 0

Related Questions