3

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!

Scott
  • 652
  • 1
  • 7
  • 15
  • you can not remove the base view or parent view. you can add your child view upon a parent view. so if you remove parent view you get the EXE_BAD error – Hiren Mar 27 '12 at 05:37
  • I'm adding the WhiteView as a child(sub) to the main view. – Scott Mar 27 '12 at 07:57

2 Answers2

5

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

Community
  • 1
  • 1
timthetoolman
  • 4,613
  • 1
  • 22
  • 22
  • Thanks Tim! Looking at your example lead me to a solution. I was only adding the view & not its controller. So I added to the original code. see above. – Scott Mar 27 '12 at 18:30
  • Tim, I have a related problem. I'm trying to have my child view controller message the parent. I've tried an adaptation of your "[(ViewController *)self.parentViewController swapViewControllers];" from your example code. It keeps telling me "No visible @interface for 'UIViewController' declares the selector 'myMethod'" I've imported the parent viewcontroller. Any suggestions? – Scott Mar 31 '12 at 20:18
  • @Scott make sure the .h file for your parent view controller contains the declaration of 'myMethod'. And then also make sure the (ViewController *) is in fact the parent view controller name that you are importing. – timthetoolman Mar 31 '12 at 20:37
  • That's it. Once again you saved the day. Thank you! – Scott Apr 01 '12 at 21:16
1

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.

Hiren
  • 12,720
  • 7
  • 52
  • 72
iLearner
  • 1,670
  • 2
  • 19
  • 45
  • I'm not sure that is the key. It's really about bringing the methods in the ViewController in the added Subview. – Scott Mar 27 '12 at 07:54