init和awakeFromNib [英] init and awakeFromNib

查看:177
本文介绍了init和awakeFromNib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想明白为什么如果我尝试设置值(即setAlphaValue或setTitle)对象(如NSButton)在init方法没有什么发生,但如果我调用setter函数在awakeFromNib它工作正常。

  @interface appController:NSObject {
NSButton * btn;
}
@end;

@implementation appController
- (void)awakeFromNib {
// it works
[btn setTitle:@My title];
}

- (id)init {
self = [super init];
if(self){
//它不工作
[btn setTitle:@My title];
}
}
@end


解决方案< 之后 之后设置 -init code>。如果你想访问插座,你需要在 -awakeFromNib 或者在插座设置后执行的另一个方法(例如 - [NSWindowController windowDidLoad



    b
  1. 分配/初始化nib文件中的对象,接收 -init -initWithFrame: -initWithCoder:

  2. 重新建立所有连接。这包括操作,发布商和绑定。

  3. -awakeFromNib 发送到接口对象,文件所有者和代理对象。 li>

您可以在资源编程指南


I'd like understand why if i try to set value (I.e. setAlphaValue or setTitle) for an object (like a NSButton) in init method nothing happen, but if i call setter function in awakeFromNib it works correctly.

@interface appController : NSObject {
    NSButton *btn;
}
@end;

@implementation appController
-(void)awakeFromNib {
   //it works
   [btn setTitle:@"My title"];
}

-(id)init { 
    self = [super init];
    if(self){
        //it doesn't works
        [btn setTitle:@"My title"];
    }
}
@end

解决方案

Outlets are set after -init and before -awakeFromNib. If you want to access outlets, you need to do that in -awakeFromNib or another method that’s executed after the outlets are set (e.g. -[NSWindowController windowDidLoad]).

When a nib file is loaded:

  1. Objects in the nib file are allocated/initialised, receiving either -init, -initWithFrame:, or -initWithCoder:
  2. All connections are reestablished. This includes actions, outlets, and bindings.
  3. -awakeFromNib is sent to interface objects, file’s owner, and proxy objects.

You can read more about the nib loading process in the Resource Programming Guide.

这篇关于init和awakeFromNib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆