Anish
Anish

Reputation: 785

Unable to remove child view controller

I am trying to add a child view controller in the function didSelectRowAtIndexPath. But i am unable to remove this child view controller that i have added. This is what i have tried :

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath
  {

    MiddleViewController *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:@"MiddleViewController"];



  if(isMenuExpanded==NO){

    isMenuExpanded=YES;
   middleVC.view.hidden=FALSE;
[middleVC.view setFrame:CGRectMake(-320, 0, self.view.frame.size.width, self.view.frame.size.height)];



[self addChildViewController:middleVC];
[self.view addSubview:middleVC.view];
[middleVC didMoveToParentViewController:self];

  [UIView animateWithDuration:0.3 animations:^{
    [middleVC.view setFrame:CGRectMake(38, 0, self.view.frame.size.width, self.view.frame.size.height)];


   }];

}else{
    isMenuExpanded=NO;


    //here i am trying to remove the child view controller
     middleVC.view.hidden=TRUE;
    [middleVC willMoveToParentViewController:nil];
    [middleVC.view removeFromSuperview];
    [middleVC removeFromParentViewController];



     }
  }

I also tried hiding the view controller.

Upvotes: 1

Views: 4607

Answers (3)

jenis
jenis

Reputation: 36

You can not hide it, because you are creating a new instance of MiddleViewController everytime you click on your cell.

What you need to do is, declare MiddleViewController *middleVC, globally (You can declare it in your .h file of the class) -

-(void)viewDidLoad
{
     MiddleViewController *middleVC =nil;
}

and in your didSelect method - replace this line -

MiddleViewController *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:@"MiddleViewController"];

with this code -

if(middleVC == nil) {
     middleVC =[self.storyboard instantiateViewControllerWithIdentifier:@"MiddleViewController"];
}

Upvotes: 1

Anand Suthar
Anand Suthar

Reputation: 3798

Please find changes, you can go in this way too

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath
{

    MiddleViewController *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:@"MiddleViewController"];

   if(isMenuExpanded==NO)
{

    isMenuExpanded=YES;
    middleVC.view.hidden=FALSE;
    [middleVC.view setFrame:CGRectMake(-320, 0, self.view.frame.size.width, self.view.frame.size.height)];


    **middleVC.view.tag=8010;**
    [self addChildViewController:middleVC];
    [self.view addSubview:middleVC.view];
    [middleVC didMoveToParentViewController:self];

    [UIView animateWithDuration:0.3 animations:^{
        [middleVC.view setFrame:CGRectMake(38, 0, self.view.frame.size.width, self.view.frame.size.height)];


    }];

}
else
{
    isMenuExpanded=NO;

    **for(UIView *view in [self.view subviews])
    {
        if(view.tag == 8010)
           [view removeFromSuperview];
    }**
}

Upvotes: 2

Jin
Jin

Reputation: 146

It seems to me that you are trying to toggle the "menu view"(middleVC) when tap happens but why are you creating a new instance every time? What I saw is you are trying to hide something that has not been added to the view.

If I were you I would take the instantiation part out of the didSelectRowAtIndexPath (to viewDidLoad or something else) and keep the reference.

Upvotes: 1

Related Questions