问题
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.
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.
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];
}
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. 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.
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]; }
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!
回答1:
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.``
Tim
回答2:
what I understood from your question is, that you want to add a subview to a superview, and which must be user interactable right?
so you can do it by following steps.
1) Add a new view to the xib.
2) make it opaque, set is alpha to less than one(but not zero, depends on you, how much trasparancy u want)
3) add the componats over it, and inside-(IBAction)showWhiteView:(id)sender
(in your case) the following code
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.
来源:https://stackoverflow.com/questions/9883820/multiple-viewcontrollers-containerview-childview-instance-of-viewcontroller