Search code examples
iosuitableviewdrop-down-menuaddsubview

Iphone remove sub view


I have a UINavigationController. On the right top i have a button on click of which i have to get a drop down table view. I created another UIViewController Class, with xib and added it as a subView to the current view. It should appear on 1st click and disappear on the 2nd click. This should happen for all click(open view and close view). I wrote this code but dont know where i'm going wrong. someone please help

-(void)modalTableView
{
tableView1 = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil];

for (UIView *subView in self.view.subviews)
{

    if ([subView isKindOfClass:[TableViewController class]]) 
    {

         [subView removeFromSuperview];
    }

    else 
    {
        [self.view addSubview:tableView1.view];

    }
  }

}

What am i missing here?

EDIT : TableViewController is the name of my UIViewController Class


Solution

  • The clue is here

    for (UIView *subView in self.view.subviews)
    

    each subView is of class UIView and your test

    isKindOfClass:[TableViewController class]
    

    is testing for class TableViewController

    I would suggest a way of doing this would be by tagging the views that you add dynamically, with say 99 - and then in your loop you can identify those views by their tag.

    eg.

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