【问题标题】:loading a nib file that is instantiated from a storyboard加载从故事板实例化的 nib 文件
【发布时间】:2012-06-08 18:41:31
【问题描述】:

所以我对这个故事板概念很陌生。我有一个视图笔尖放入情节提要中,每个笔尖都对应于我拥有的 UIViewController 子类,我尝试使用以下代码加载笔尖文件:

TestViewController *vc = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
    [self.view setBackgroundColor:[UIColor blueColor]];
    [self.view setFrame:CGRectMake(0, self.profilePicture_.frameHeight + self.profilePicture_.frameY + 10, self.scrollView_.frameWidth, 100)];
    [self.view addSubview:vc.view];

但是,它给了我以下错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/aditya15417/Library/Application Support/iPhone Simulator/5.1/Applications/64E6CEC9-E6DC-4AF5-BF16-11BFB6415BDC/Pulse.app> (loaded)' with name 'TestViewController''

所以问题是,如果我在故事板上有我的笔尖,就不能使用 initWithNibName 吗?如果有办法,我该怎么做?

【问题讨论】:

    标签: iphone objective-c ios ipad


    【解决方案1】:

    这就是我的做法:

    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
    

    【讨论】:

      【解决方案2】:

      请使用

      [self.storyboard instantiateViewControllerWithIdentifier:@"some identifier you set in IB"];
      

      【讨论】:

      • 是否可以设置实例类(以编程方式)?
      【解决方案3】:

      当您使用故事板时,您不会直接使用单个 nib,也不会自己实例化视图控制器。您可以使用 UIStoryboard 类从故事板 as described in the documentation 实例化视图控制器。

      【讨论】:

        【解决方案4】:

        为什么将 PNRNewsfeedViewController 加载为 TestViewController? TestViewController 是 PNRNewsfeedViewController 的基类吗?确保在 nib 中,将 File's Owner 自定义类设置为 PNRNewsfeedViewController,然后分配一个新闻提要视图控制器:

        PNRNewsfeedViewController *vc = [[PNRNewsfeedViewController alloc] initWithNibName:@"PNRNewsfeedViewController" bundle:nil];
        // ...
        

        【讨论】:

        • 对不起..是的..那只是一个错字..所以我实际上使用了 TestViewController
        猜你喜欢
        • 2015-08-22
        • 1970-01-01
        • 2015-03-26
        • 2012-01-17
        • 1970-01-01
        • 1970-01-01
        • 2015-06-07
        • 2015-10-19
        • 1970-01-01
        相关资源
        最近更新 更多