Iphone remove sub view

Try this,

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

     [subView removeFromSuperview];
}

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

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?


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