2012-07-27 44 views
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,我把我的updateLabelinit日志我看到Update!!中,但NSTextField它不符合我的文字更新。 但是,当我按下按钮,调用相同updateLabelNSTextField更新。有人能帮我理解为什么它不能按预期工作吗?Objective-C的更新的NSTextField初始化

+0

您是否尝试在init方法中放入一条日志语句以确保它被调用? – rdelmar 2012-07-27 02:22:48

+0

我说'init'打印出来'NSLog(@“Update !!%@”);'但是文本中的字段是一样的 – Constantin 2012-07-27 02:24:40

+0

对不起,错过了。这可能是因为在init方法中这样做在这个过程中为时尚早。你如何加载视图控制器的视图?你在实现loadView吗?如果是这样,请尝试将调用放入updateLabel中。 – rdelmar 2012-07-27 02:33:33

回答

0

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

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

- (void)loadView 
{  
    [super loadView]; 
    [self updateLabel];  
}