1

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?

Smittey
  • 2,475
  • 10
  • 28
  • 35
Rupinder
  • 156
  • 1
  • 12

2 Answers2

1

Step 1.

In CenterViewController.h file create a object for InventoryViewController class

InventoryViewController *invent;

Step 2. In CenterViewController.m file

Create following method

-(void)addInventoryViewController

{

invent = [[InventoryViewController alloc]       
  initWithNibName:@"InventoryViewController" bundle:nil];

[invent.view setFrame:CGRectMake(400,100,320,200)];
[self addChildViewController:invent];
[self.view addSubview:invent.view];
[invent didMoveToParentViewController:self];
}

Step 3. On you button click action method

-(IBAction)onButtonClick:(id)sender

{

if(invent != nil)
  {

 [invent.view removeFromSuperview];
 invent = nil;



}



 else

  {

     [self addInventoryViewController];

  }

}
Darshan
  • 2,272
  • 3
  • 31
  • 43
Mobile Apps Expert
  • 1,028
  • 1
  • 8
  • 11
0

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

Rupinder
  • 156
  • 1
  • 12