我有这个级联:
应用中的某处
- (void) go
{
MyCustomViewController* controller = [[MyCustomViewController alloc] init];
controller.delegate = self;
[controller run];
}
在 MyCustomViewController 中
- (id) init
{
// there is an if statement here in real to choose another XIB if needed
// but I only display here the code that is called in my tests
self = [super initWithNibName:@"MyXIB" bundle:nil];
if (!self) return nil;
self.delegate = nil;
return self;
}
- (void) run
{
// do things with self.view
}
MyCustomViewController 继承GenericWindowController
在 GenericWindowController 中
/*- (id) initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
if (!(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) return nil;
self.view.userInteractionEnabled = NO; // THE APP CRASHES HERE ! self.view is nil
...
return self;
}*/
// METHOD created following first answers : NOT CALLED
- (void) viewDidLoad
{
self.view.userInteractionEnabled = NO;
// and many other things done with self.view
}
MyXIB 的文件所有者设置为 MyCustomViewController 并且 View 已连接。
所有文件都包含并 checkin 项目。
GenericWindowController 旨在制作一些标准的东西。
MyCustomViewController 扩展了这些内容以使用自定义 View ,如 MyXIB 中所设计的那样。
为什么 self.view 在 GenericWindowController 中为 nil?
为什么 viewDidLoad
没有被调用?
最佳答案
View Controller 不应尝试访问其在 initWithNibName:bundle:
中的 View 。它应该等到 viewDidLoad
。
关于iPhone - self.view 在 initWithNibName 子类中为 nil,并且未调用 viewDidLoad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11854556/