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

我想在显示视图时更新一个 NSTextField,我把我updateLabel的 at放在我init看到的日志中,Update!!NSTextField它没有用我的文本更新。但是当我按下调用相同的按钮时,它updateLabelNSTextField更新的。有人可以帮我理解为什么它没有按预期工作吗?

4

1 回答 1

0

我按照@rdelmar 的建议使用loadView. 谢谢你。

如果有人感兴趣,这里是如何实现它。

- (void)loadView
{       
    [super loadView];
    [self updateLabel];    
}
于 2012-07-27T02:59:55.633 回答