多个ViewControllers(containerView?childView?viewController的实例?)

我需要在另一个顶部添加一个新视图(w / ViewController)。 用户暂时与此新视图交互,然后我想删除它。 在较旧版本的Xcode中,我能够将其添加为子视图。 我现在得到一个EXC_BAD_ACCESS错误。

我不希望将添加的视图作为模态。 我需要通过添加的视图查看原始背景。 我已经阅读了很多关于新的自定义containerViews,addChildView和presentView的内容。 我看不出其中任何一个都是明确的答案。

这是以前工作的旧代码 – 主ViewController中的Action:

-(IBAction)showWhiteView:(id)sender { WhiteViewController *whiteView = [[WhiteViewController alloc] initWithNibName:@"WhiteViewController" bundle:nil]; [self.view addSubview:whiteView.view]; } 

在添加的视图中删除它的操作:

 -(IBAction)removeView:(id)sender { [self.view removeFromSuperview]; } 

谢谢你的帮助。

也许一个VISUAL EXAMPLE将有助于解释 – 假设主视图是海洋,动画波浪和云移动由MainView控制器控制。 用户点击了一些东西,我想在主视图中添加一条船(WhiteView)。 我希望用户与船进行交互:点击这里帆打开,点击那里锚点等等(需要WhiteViewController的方法)最终我想从海洋中移除船。

谢谢Tim – 新增代码:

 -(IBAction)showWhiteView:(id)sender { WhiteViewController *whiteView = [[WhiteViewController alloc] initWithNibName:@"WhiteViewController" bundle:nil]; [self addChildViewController:whiteView]; [whiteView didMoveToParentViewController:self]; [self.view addSubview:whiteView.view]; } 

并在WhiteViewController中删除:

 -(IBAction)removeView:(id)sender { [self.view removeFromSuperview]; [self removeFromParentViewController]; } 

我期待着有关改善这一点的任何进一步建议。 谢谢大家!

请参阅此处有关UIViewController包含的答案。 我在这里放了一个关于UIViewController包含的示例项目: http : //github.com/toolmanGitHub/stackedViewControllers

希望这有助于.`

蒂姆

我从你的问题中理解的是,你想在superview中添加一个子视图,哪个必须是用户可以互动的?

所以你可以按照以下步骤来做到这一点。

1)向xib添加新视图。
2)使它不透明,设置为小于1的alpha(但不是零,取决于你,你想要多少trasparancy)
3)在它上面添加componats,并在里面-(IBAction)showWhiteView:(id)sender (在你的情况下)下面的代码

 whiteView.frame = CGRectMake(55, 60, 200, 200); [UIView beginAnimations:@"" context:nil]; [UIView setAnimationDuration:.5]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES]; [self.view addSubview:whiteView]; 

要删除它,请执行以下操作

 -(IBAction)removeView:(id)sender { [whiteView removeFromSuperview]; } 

别忘了连接新添加的视图。

试试看。