gpt4 book ai didi

ios - 如何使 initWithNibName 表现得像 instantiateViewControllerWithIdentifier

转载 作者:行者123 更新时间:2023-11-29 04:12:50 24 4
gpt4 key购买 nike

我正在尝试添加 View Controller 容器 libraryremail电子邮件客户端。这个想法是 View Controller 容器在两层中呈现其 subview Controller 。它提供了滑动顶部 View 以显示其下方 View 的功能。

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

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

其中slidingViewController View Controller 容器的顶级实例。通过此实例,您可以在顶 View 下方设置 View Controller 并添加平移。

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

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

但是使用该代码..我收到此错误:
-[__NSArrayM insertObject:atIndex:]:对象不能为 nil

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

查看文档..我发现 instantiateViewControllerWithIdentifier 之间存在差异和 initWithNibName :前者始终返回一个对象。而后者仅在第一次访问 View Controller 的 View 时加载。

问题:如何使 initWithNibName 返回加载的 View Controller 对象,无论该 View 是否已被访问。类似于 instantiateViewControllerWithIdentifier?

最佳答案

您应该能够通过 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;
}

关于ios - 如何使 initWithNibName 表现得像 instantiateViewControllerWithIdentifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14170220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com