【发布时间】:2011-09-06 16:38:07
【问题描述】:
所以我有一个 NSViewController (MyVC) 设置如下:
//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 的设置绝对正确。
【问题讨论】:
-
您是否尝试在
viewDidLoad中不使用 NSLog?在init中,它将始终为 NULL。在loadView我不记得了。 -
NSViewController(注意,不是 UIViewController)没有 -viewDidLoad。 (stackoverflow.com/questions/4492485/…)
-
实际的
IBOutlet声明在哪里? -
property 和 ivar 会不会不同,IBOutlet 设置错了?如果您将
self.input登录到loadView会发生什么?
标签: xcode cocoa null iboutlet automatic-ref-counting