Iphone remove sub view
Asked Answered
G

4

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

Gussie answered 22/3, 2012 at 10:9 Comment(0)
T
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];
    }
}
Trail answered 22/3, 2012 at 10:14 Comment(0)
M
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?

Masonmasonic answered 19/6, 2015 at 6:32 Comment(0)
B
4

Try this,

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

     [subView removeFromSuperview];
}
Baudoin answered 22/3, 2012 at 10:17 Comment(2)
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 asubviewGussie
Is your if condition working properly. developer.apple.com/library/mac/#documentation/Cocoa/Reference/…:Baudoin
T
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];
    }
}
Trail answered 22/3, 2012 at 11:25 Comment(2)
its just going to the second if loop where its adding a subview. n yes tableview1 is retained in its properties.Gussie
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?Trail

© 2022 - 2024 — McMap. All rights reserved.