我正在努力在用户开始使用我的应用程序之前进行用户调查,这通常是我研究的数据集合。
我的问题是在主视图 Controller 的顶部添加 subview 。
如您在第一个屏幕截图中所见,我有两个 subview Controller 。我想在主视图 Controller 上添加第一个 subview ,每当用户单击下一个自定义按钮时,第一个 subview 消失,第二个 subview 出现在主视图 Controller 的顶部。
firstsubview实现如下:
CGRect rect = [firstSurveyViewController.view frame];
rect.origin.x = 5;
rect.origin.y = 5;
[firstSurveyViewController.view setFrame:rect];
[self.view addSubview:firstSurveyViewController.view];
但我想删除第一个 subview 并在用户单击下一步按钮时添加第二个 subview 。 我该如何实现?
最佳答案
为了组合模态视图 Controller ,您有两个真正的选择(忽略 NDA 下的酷东西):
使用 Container View Controller pattern将内部 View Controller 插入其父级。
使用该 View Controller 添加一个新窗口,类似于您在 AppDelegate 中的操作方式。创建窗口,将您的 child 添加为 Root View Controller ,使其成为关键且可见。这实际上是
UIAlertView
为您的应用执行类似行为所做的。
我不能说什么对你的情况更好,但我在容器 View Controller 方面有更多经验,所以我会给你重点。要添加 subview ,您需要在父 View Controller 中执行以下步骤(来自上面的链接):
- (void) displayContentController: (UIViewController*) content;
{
[self addChildViewController:content]; // 1
content.view.frame = [self frameForContentController]; // 2
[self.view addSubview:self.currentClientView]; // 3
[content didMoveToParentViewController:self]; // 4
}
这将:
- 添加您的 subview Controller 。
- 将框架设置为任何你想要的。如果您希望它占据整个空间,这可以是
self.view.bounds
。 - 将 View 添加到其父级。
- 通知 subview Controller 它已被添加。
要删除 subview Controller ,您可以从链接中执行相反的操作:
- (void) hideContentController: (UIViewController*) content
{
[content willMoveToParentViewController:nil]; // 1
[content.view removeFromSuperview]; // 2
[content removeFromParentViewController]; // 3
}
这将:
- 通知您的 subview Controller 它将消失。
- 从视觉堆栈中移除它的 View 。
- 将其从其父项中移除。
关于ios - 在 MainViewController 顶部添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17619456/