【问题标题】:how to make initWithNibName behave like instantiateViewControllerWithIdentifier如何使 initWithNibName 表现得像 instantiateViewControllerWithIdentifier
【发布时间】:2013-01-05 08:54:32
【问题描述】:

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

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

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

其中 slidingViewControllerthe 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?

【问题讨论】:

    标签: ios xcode storyboard nib uistoryboard


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-06
      • 2012-11-24
      • 2017-06-19
      • 2017-01-26
      • 2016-01-15
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      相关资源
      最近更新 更多