多个 ViewControllers(containerView?childView?viewController 的实例?) [英] multiple ViewControllers (containerView? childView? instance of viewController?)
问题描述
我需要在另一个视图之上添加一个新视图(带 ViewController).用户与这个新视图交互了一段时间,然后我想删除它.在旧版本的 Xcode 中,我能够将其添加为子视图.我现在收到 EXC_BAD_ACCESS 错误.
I need to have a new view(w/ ViewController) added over the top of another. The user interacts with this new view for a while, and then I want to remove it. In an older version of Xcode I was able to add it as a subview. I now get an EXC_BAD_ACCESS error.
我不想将添加的视图作为模态.我需要通过添加的视图查看原始背景.我已经阅读了很多关于新的自定义 containerViews、addChildView 和 &当前视图.我看不出其中任何一个是明确的答案.
I don't want the added view as a modal. I need to see the original background through the added view. I've read a lot about the new custom containerViews, addChildView, & presentView. I can't see that any of these are the clear answer.
这是以前有效的旧代码 -主 ViewController 中的操作:
Here's the old code that worked before - Action in the main ViewController:
-(IBAction)showWhiteView:(id)sender
{
WhiteViewController *whiteView = [[WhiteViewController alloc] initWithNibName:@"WhiteViewController" bundle:nil];
[self.view addSubview:whiteView.view];
}
在添加的视图中删除它的操作:
Action in the added view to remove it:
-(IBAction)removeView:(id)sender
{
[self.view removeFromSuperview];
}
感谢您的帮助.
也许一个可视化示例将有助于解释 - 假设主视图是一个海洋,动画波浪和云由 MainView Controller 控制.用户点击一些东西,然后我想在主视图中添加一条船(WhiteView).我希望用户与船进行交互:点击这里帆打开,点击那里锚点下降等(需要 WhiteViewController 的方法)最终我想从海洋中移除船.
Maybe a VISUAL EXAMPLE will help explain - Let's say the main view is an ocean, with animated waves and clouds moving controlled by MainView Controller. The user taps something and a I want to add a boat(WhiteView) to the main view. I want the user to interact with boat: tap here the sail opens, tap there the anchor drops, etc. (needing the methods of the WhiteViewController) Eventually I want to remove the boat from the ocean.
感谢 Tim - 添加了新代码:
Thanks Tim - New code added:
-(IBAction)showWhiteView:(id)sender
{ WhiteViewController *whiteView = [[WhiteViewController alloc] initWithNibName:@"WhiteViewController" bundle:nil];
[self addChildViewController:whiteView];
[whiteView didMoveToParentViewController:self];
[self.view addSubview:whiteView.view]; }
并在 WhiteViewController 中删除:
and within the WhiteViewController to remove:
-(IBAction)removeView:(id)sender
{ [self.view removeFromSuperview];
[self removeFromParentViewController]; }
我期待任何进一步的改进建议.谢谢大家!
I look forward to any further suggestions on making this better. Thanks all!
推荐答案
查看答案 此处 关于 UIViewController 的包含.我在这里整理了一个关于 UIViewController 包含的示例项目:http://github.com/toolmanGitHub/stackedViewControllers
See the answer here concerning UIViewController containment. I put together an example project on UIViewController containment here: http://github.com/toolmanGitHub/stackedViewControllers
希望这会有所帮助.``
Hope this helps.``
蒂姆
这篇关于多个 ViewControllers(containerView?childView?viewController 的实例?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!