0
@interface TestViewController : NSViewController
@property (nonatomic, retain) IBOutlet NSTextField *myLabel;

- (IBAction)sendMessage:(NSButton *)sender;

@end


@implementation TestViewController
@synthesize myLabel = _myLabel;

- (id)init{
    self = [super init];
    if(self){
        [self updateLabel];
    }
    return self;
}

- (IBAction)sendMessage:(NSButton *)sender {
    [self updateLabel];
    NSLog(@"Message sent");
}

- (void) updateLabel{
    NSLog(@"Update!! %@");
    [self.myLabel setStringValue:@"random text"];
}

@end

I want to update an NSTextField when view is displayed, and i put my updateLabel at init in the log i see Update!! but the NSTextField it's not update with my text. But when i press the button that calls the same updateLabel the NSTextField is updatet. Can someone help me to understand why it's not working as expected ?

Constantin
  • 2,288
  • 2
  • 24
  • 31
  • Did you try putting a log statement in your init method to make sure it's being called? – rdelmar Jul 27 '12 at 02:22
  • i said that at `init` this is printed out `NSLog(@"Update!! %@");` but text in the field is the same – Constantin Jul 27 '12 at 02:24
  • Sorry, missed that. It could be that doing this in an init method is too early in the process. How are you loading the view controller's view? Are you implementing loadView? If so, try putting the call to updateLabel in there. – rdelmar Jul 27 '12 at 02:33
  • thank you `loadView` did the job :D – Constantin Jul 27 '12 at 02:46

1 Answers1

0

I follow the suggestion of @rdelmar to use loadView. Thank you.

And here is how to implement it if anyone interested.

- (void)loadView
{       
    [super loadView];
    [self updateLabel];    
}
Constantin
  • 2,288
  • 2
  • 24
  • 31