ios Iphone 删除子视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9820113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:28:51  来源:igfitidea点击:

Iphone remove sub view

iosuitableviewdrop-down-menuaddsubview

提问by Sharanya K M

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

我有一个 UINavigationController。在右上角,我有一个单击按钮,我必须获得一个下拉表格视图。我使用 xib 创建了另一个 UIViewController 类,并将其作为子视图添加到当前视图中。它应该在第一次点击时出现,在第二次点击时消失。这应该发生在所有点击(打开视图和关闭视图)上。我写了这段代码,但不知道我哪里出错了。有人请帮忙

-(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

编辑:TableViewController 是我的 UIViewController 类的名称

回答by Damo

The clue is here

线索在这里

for (UIView *subView in self.view.subviews)

each subView is of class UIView and your test

每个子视图都属于 UIView 类和您的测试

isKindOfClass:[TableViewController class]

is testing for class TableViewController

正在测试 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.

我建议这样做的一种方法是标记您动态添加的视图,比如 99 - 然后在您的循环中,您可以通过它们的标签识别这些视图。

eg.

例如。

for (UIView *subView in self.view.subviews)
{
    if (subView.tag == 99) 
    {
        [subView removeFromSuperview];
    }
}

回答by Suragch

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?

来源:从父视图/超级视图中删除所有视图的最佳方法是什么?

回答by chinthakad

Try this,

尝试这个,

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

     [subView removeFromSuperview];
}

回答by Damo

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

这里有一些应该以某种方式工作的东西 - 假设 tableView1 是一个保留的@property(如果不是,那么这个关于延迟加载技术的答案可能适合你)。

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