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
If you add a tag to your view you can remove a specific view. why not view. subviews. filter({$0.
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]; } }
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With