39

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

Sharanya K M
  • 1,805
  • 4
  • 23
  • 44

4 Answers4

68

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];
    }
}
Damo
  • 12,840
  • 3
  • 51
  • 62
14

Swift version

To remove a single subview:

subView.removeFromSuperview()

To remove all subviews:

for subView in self.subviews as [UIView] {
    subView.removeFromSuperview()
}

Source: What is the best way to remove all views from parent view / super view?

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
4

Try this,

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

     [subView removeFromSuperview];
}
chinthakad
  • 939
  • 2
  • 16
  • 31
  • I have a table view in my view also. So what this is doing is removing the tableview in my view and not the view controller i have added as asubview – Sharanya K M Mar 22 '12 at 10:32
  • Is your if condition working properly. http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/isKindOfClass: – chinthakad Mar 22 '12 at 10:50
3

Here is something that should go some way to working - assuming that tableView1 is a retained @property (If not then maybe this SO answer on lazy loading techniques is for you).

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

    if (tableView1.view.superview == nil)
    {
        [self.view addSubview:tableView1.view];
    } else
    {
        [tableView1.view removeFormSuperview];
    }
}
Community
  • 1
  • 1
Damo
  • 12,840
  • 3
  • 51
  • 62
  • its just going to the second if loop where its adding a subview. n yes tableview1 is retained in its properties. – Sharanya K M Mar 22 '12 at 11:51
  • So, if it has a superview already you must have added it to something previously....keep checking you must be nearly there. Did you want to post your revised code now? – Damo Mar 22 '12 at 11:58