2011-09-06 89 views
0

不设定所以我得建立像这样的NSViewController(MyVC):IBOutlets在NSViewController

//MyVC.h 
... 
@property (nonatomic, retain) IBOutlet NSTextField *input; 
... 

//MyVC.m 
... 
@synthesize input; 

- (id)init 
{ 
    self = [super initWithNibName: @"MyVC" bundle: [NSBundle mainBundle]]; 
    NSLog(@"%@", input); //prints (null) always 
    return self; 
} 

- (void)loadView 
{ 
    [super loadView]; 
    NSLog(@"%@", input); //still (null) 
} 
... 

//MyVC.xib 

Custom View  [Referencing Outlet: File's Owner.view] 
    Text Field [Referencing Outlet: File's Owner.input] 

现在,当我打开这个NSViewController(由MyVC *vc = [[MyVC alloc] init];方式),并将其加载到一个窗口,我适当地看到了文本字段。但是,正如上面的粘贴(和几个BAD_ACCESSes)所建议的,vc.input永远不会正确指向文本字段。

注:

  • 该项目正在运行ARC。
  • 这不是一个简化或泛化。我已经运行这个确切的代码无济于事。
  • 所有的IBOutlets都是正确设置的。
+0

你是否在'viewDidLoad'中尝试了没有NSLog?在'init'中它将始终为NULL。在'loadView'中,我不记得了。 – Nekto

+2

NSViewController(注意,不是UIViewController)没有-viewDidLoad。 (http://stackoverflow.com/questions/4492485/when-programming-for-mac-os-x-is-there-an-equivalent-to-viewdidload) –

+1

实际的'IBOutlet'声明在哪里? – 2011-09-06 17:03:36

回答

1

错误是一个组合。

我的一个修订是缺少IBOutlet标记,并且它们都没有在运行时保留对ViewController的引用。