Search code examples
childviewcontrollerparentviewcontroller

Remove Childview controller from parent viewController in ios


I have a Parent viewController named "CenterViewConroller" and Child viewController named "InventoryViewController". I have a UIButton in CenterViewController, when i click the Button childView should appear at given dimension and when i again click on parentcontroller child view should disappear.

I am adding child view on button click as:

InventoryViewController *invent = [[InventoryViewController alloc] initWithNibName:@"InventoryViewController" bundle:nil];
     // iRosaAppDelegate_iPhone *appDelegate = [UIApplication sharedApplication].delegate;
    [invent.view setFrame:CGRectMake(400,100,320,200)];
    [self addChildViewController:invent];
    [self.view addSubview:invent.view];
    [invent didMoveToParentViewController:self];

How do i remove ChildView on same button click?


Solution

  • In viewDidLoad we can call ChildViewController and make the view hidden and when the button is clicked we can toggle the hidden property as follow.

    //IN VIEWDIDLOAD invent = [[InventoryViewController alloc] initWithNibName:@"InventoryViewController" bundle:nil]; [invent.view setFrame:CGRectMake(400,100,320,200)]; [self.view addSubview:invent.view]; invent.view.hidden = true;

    //ON BUTTON CLICK Func

    if (self.invent.view.hidden == true) { [self.invent.view setHidden:NO]; } else { [self.invent.view setHidden:YES]; }