iPhone - self.view 在 initWithNibName 子类中为 nil,并且未调用 viewDidLoad

标签 iphone ios uiview uiviewcontroller viewdidload

我有这个级联:

应用中的某处

- (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/

相关文章:

ios以编程方式创建 View

ios - 核心数据 - 1600 错误(删除具有特定删除规则的对象时)

ios - 设置UIView根据UILabel调整大小

iphone - 连续绘制 iPhone 位图对象?我缺少什么?

iphone - cocos2d 中 ipad 视网膜中的游戏图形模糊

ios - 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因 : '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array

ios - 将标签放在 spaceBetweenPoints 中的每个点下

ios - 如何以编程方式更改我的 viewController 的 View ?

iphone - UIButton - 我们应该释放还是不释放?

objective-c - NSOperationQueue 和 UITableView 版本使我的应用程序崩溃