简体   繁体   English

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

[英]multiple ViewControllers (containerView? childView? instance of viewController?)

I need to have a new view(w/ ViewController) added over the top of another. 我需要在另一个顶部添加一个新视图(w / ViewController)。 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. 在较旧版本的Xcode中,我能够将其添加为子视图。 I now get an EXC_BAD_ACCESS error. 我现在收到一个EXC_BAD_ACCESS错误。

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. 我已经阅读了很多关于新的自定义containerViews,addChildView和presentView的内容。 I can't see that any of these are the clear answer. 我看不出其中任何一个都是明确的答案。

Here's the old code that worked before - Action in the main ViewController: 这是以前工作的旧代码 - 主ViewController中的Action:

-(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];
}

Thanks for your help. 谢谢你的帮助。

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. 也许一个VISUAL EXAMPLE将有助于解释 - 假设主视图是海洋,动画波浪和云移动由MainView控制器控制。 The user taps something and a I want to add a boat(WhiteView) to the main view. 用户点击了一些东西,我想在主视图中添加一条船(WhiteView)。 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. 我希望用户与船进行交互:点击这里帆打开,点击那里锚点等等(需要WhiteViewController的方法)最终我想从海洋中移除船。

Thanks Tim - New code added: 谢谢Tim - 新增代码:

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

and within the WhiteViewController to remove: 并在WhiteViewController中删除:

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

I look forward to any further suggestions on making this better. 我期待着有关改善这一点的任何进一步建议。 Thanks all! 谢谢大家!

See the answer here concerning UIViewController containment. 请参阅此处有关UIViewController包含的答案。 I put together an example project on UIViewController containment here: http://github.com/toolmanGitHub/stackedViewControllers 我在这里放了一个关于UIViewController包含的示例项目: http//github.com/toolmanGitHub/stackedViewControllers

Hope this helps.`` 希望这有助于.`

Tim 蒂姆

what I understood from your question is, that you want to add a subview to a superview, and which must be user interactable right? 我从你的问题中理解的是,你想在superview中添加一个子视图,哪个必须是用户可以互动的?

so you can do it by following steps. 所以你可以按照以下步骤来做到这一点。

1) Add a new view to the xib. 1)向xib添加新视图。
2) make it opaque, set is alpha to less than one(but not zero, depends on you, how much trasparancy u want) 2)使它不透明,设置为小于1的alpha(但不是零,取决于你,你想要多少trasparancy)
3) add the componats over it, and inside -(IBAction)showWhiteView:(id)sender (in your case) the following code 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];

And to remove it do the following 要删除它,请执行以下操作

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

Dont forget to connect the newly added view. 别忘了连接新添加的视图。

try it out. 试试看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM