gpt4 book ai didi

ios - UIViewController 包含崩溃

转载 作者:行者123 更新时间:2023-11-29 13:33:50 25 4
gpt4 key购买 nike

我正在摆弄一些代码来尝试让 UIViewController 包含工作(在 iPad 应用程序中)。

场景如下:我有一个 View ,我想在其上显示第二个 View 。我想让第二个(不同的)UIViewController 管理与第二个 View 的所有交互。第一个 View 将在按下按钮时加载第二个 View 。当按下按钮时,以下代码在第一个 Controller 中运行:

-(void)displayPropertyView // <-button triggers this
{
// Need to instantiate a new view and add it to the main editor view.
HPSQuestionListController* questionListController = [ [ HPSQuestionListController alloc ] init ];
[self.view addSubview:questionListController.view];
[self.view bringSubviewToFront:questionListController.view];
[self pushViewController:questionListController animated:YES];

}
-(void)pushViewController:(UIViewController*)controller animated:(BOOL)animated
{
[self addChildViewController:controller];
//[controller didMoveToParentViewController:self];

if (YES)
{
[self transitionFromViewController:self
toViewController:controller
duration:1.0
options:nil
animations:^{}
completion:^(BOOL finished){
[controller didMoveToParentViewController:self];
}];
}
}

应用程序在 transitionFromViewController 行崩溃:

Children view controllers and must have a common parent view controller when calling -[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]'

有人知道怎么解决吗?我对 UIViewController 包含的理解是我可以让两个 Controller 同时管理两个 View 。这是错误的吗?

最佳答案

subview Controller 应该有相同的父容器 View Controller (这就是错误消息所说的)。要添加 subview Controller ,请在容器 View Controller 中使用此代码:

[self addChildViewController:myVC1];
[myVC1 didMoveToparentViewController:self];

在容器 View Controller 中你可以使用transitionFromViewController

[self transitionFromViewController:myVC1 toViewController:myVC2 duration:2.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{} completion:^(BOOL finished) {
[myVC1 removeFromParentViewController];
[myVC2 didMoveToParentViewController:self];
}];

您正在尝试做一些没有多大意义的事情,self 不是您代码中的容器 View Controller 。

有关完整示例,请参阅:https://github.com/toolmanGitHub/stackedViewControllers

关于ios - UIViewController 包含崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11157576/

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