如何使initWithNibName像instantiateViewControllerWithIdentifier一样运行

时间:2013-01-05 08:54:32

标签: ios xcode storyboard nib uistoryboard

我正在尝试将视图控制器容器library添加到remail电子邮件客户端。想法是视图控制器容器presents its child view controllers in two layers. It provides functionality for sliding the top view to reveal the views underneath it.

在库描述中,这是将子控制器View附加到其父级的建议方法:

if (![self.slidingViewController.underLeftViewController 
        isKindOfClass:[MenuViewController class]]) {
    self.slidingViewController.underLeftViewController  = 
        [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
  }

其中 slidingViewController the top-level instance of the view controller container. With this instance, you can set the view controllers underneath the top view and add panning.

我正在使用xib文件,而不是商店。所以我的代码看起来像这样:

if (![self.slidingViewController.underLeftViewController 
        isKindOfClass:[MenuViewController class]]) {
    self.slidingViewController.underLeftViewController  =
         [[MenuViewController alloc] initWithNibName:@"Menu" bundle:nil];
}

但使用该代码..我收到此错误:
-[__NSArrayM insertObject:atIndex:]: object cannot be nil

可以追溯到slidingViewController执行以下操作:
[self.view insertSubview:_underTopViewController.view atIndex:0];

查看文档..我发现instantiateViewControllerWithIdentifierinitWithNibName之间存在差异:前者始终返回一个对象..后者仅加载the first time the view controller’s view is accessed

问题:如何使initWithNibName返回已加载的viewcontroller对象,无论是否访问过该视图..类似于instantiateViewControllerWithIdentifier?

1 个答案:

答案 0 :(得分:1)

您应该可以通过just accessing the view property触发它,例如;

if (![self.slidingViewController.underLeftViewController 
              isKindOfClass:[MenuViewController class]]) 
{
    MenuViewController *vc = 
      [[MenuViewController alloc] initWithNibName:@"Menu" bundle:nil];
    [vc view];  // <-- access the view and trigger loading
    self.slidingViewController.underLeftViewController  = vc;
}