Multiple ViewControllers (containerViewView? ChildView? Instance of viewController?)

I need to have a new View (with a ViewController) added on top of the other. The user interacts with this new view for a while, and then I want to delete it. In the older version of Xcode, I was able to add it as a subtitle. Now I am getting EXC_BAD_ACCESS error.

I don't want the added view to be modal. I need to see the original background through the added view. I've read a lot about new custom containers, addChildView and presentView. I don't see any of these being a 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 added to remove it:

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

      

Thank you for your help.

Perhaps a VISUAL EXAMPLE will help explain - let's say the main view is the ocean, with animated waves and clouds controlled by the MainView controller. User clicks on something and I want to add a boat (WhiteView) to the main view. I want the user to interact with the boat: click here to open the sail, click there anchor drops, etc. (You need to use WhiteViewController methods). Eventually I want to remove the boat from the ocean.

Thanks Tim - Added new code:

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

      

and inside WhiteViewController for deletion:

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

      

I am looking forward to further suggestions for improving this. Thanks everyone!

+3


source to share


2 answers


See the answer here regarding securing the UIViewController. I put together a sample project on the content of the UIViewController here: http://github.com/toolmanGitHub/stackedViewControllers

Hope it helps. ``



Tim

+5


source


What I figured out from your question is that you want to add a subview to the supervisor and that should be properly interacting with the user?

so you can do it by following the steps below.

1) Add a new view to xib.
  2) make it opaque, set alpha less than one (but not zero, it is up to you how much trasparancy you want)
 3) add components to 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];
  }

      

Don't forget to connect the newly added view.

try it.

+1


source







All Articles