UIViewController initWithNibName结果为nil [英] UIViewController initWithNibName results in nil view

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

问题描述

我正在尝试制作一个视图控制器,该视图控制器将始终加载相同的笔尖.我的初始化代码如下:

I'm trying to make a view controller that will always load the same nib. My initialization code looks like this:

-(id)init
{
    NSString* nibName;

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        nibName = @"XIB_iPad";
    else
        nibName = @"XIB_iPhone";

    self = [super initWithNibName:nibName bundle:nil];
    if (self)
    {
        ...
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    return [self init];
}

问题在于此代码导致视图控制器具有nil视图.我在这里做错什么了吗?有更好的方法吗?

The problem is that this code results in the view controller having a nil view. Is there something I'm doing wrong here? Is there a better way to do this?

谢谢.

推荐答案

检查以下内容:

  1. 您的项目中同时存在XIB_iPad.xib和XIB_iPhone.xib.
  2. 每个xib文件的文件所有者"具有正确的类(YourViewController).
  3. 每个文件所有者视图出口(在xib文件中)已正确链接 查看.
  1. Both XIB_iPad.xib and XIB_iPhone.xib exist in your project.
  2. Each xib file's "File owner" is of the correct class (YourViewController).
  3. Each File Owner view outlet (in the xib files) is linked correctly to a view.

如果在检查完这三个步骤后仍然遇到问题,请提供用于实例化和设置自定义视图控制器的代码.

If you keep having problems after you check these three steps, please provide with the code you use to instantiate and setup your custom view controller.

这篇关于UIViewController initWithNibName结果为nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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