gpt4 book ai didi

ios - 如何将 subview 添加为 View Controller View 的 subview ?

转载 作者:行者123 更新时间:2023-11-29 02:55:15 24 4
gpt4 key购买 nike

到目前为止,我已经向我的主视图 Controller 添加了一个 subview 。但是我希望这个 subview 成为我的 View Controller View 的 subview ,所以我尝试这样做

[self.view addSubview:_converterViewController.view]; 

但这给了我这个错误:

libc++abi.dylib: terminating with uncaught exception of type NSException

我知道这真的很模糊,但出于某种原因,这就是我所能得到的。那我做错了什么?这是我与此任务相关的代码,它都位于我的 View Controller viewDidAppear 方法中。

[self addChildViewController:_converterViewController];
[_converterViewController didMoveToParentViewController:self];
[self.view addSubview:_converterViewController.view];//this line produces the error

感谢您的帮助。

更新错误:

'NSInternalInconsistencyException', reason: 'Could not load NIB...

最佳答案

添加childViewController需要做的步骤如下

UIViewController *viewController = [UIViewController new];

[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];

1 - 创建 subview Controller
2 - 添加是作为一个 child
3 - 将它的 View 添加到 View 层次结构4 - 通知 childViewController 它已被添加到父级。

关于ios - 如何将 subview 添加为 View Controller View 的 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23983861/

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