Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 732
Sharanya K M Avatar asked Mar 22 '12 10:03

Sharanya K M


People also ask

How do I remove a subview from a view in Swift?

If you add a tag to your view you can remove a specific view. why not view. subviews. filter({$0.


2 Answers

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];     } } 
like image 180
Damo Avatar answered Oct 03 '22 00:10

Damo


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?

like image 44
Suragch Avatar answered Oct 03 '22 01:10

Suragch