【问题标题】:How to remove everything from superview [duplicate]如何从superview中删除所有内容[重复]
【发布时间】:2014-02-17 20:04:40
【问题描述】:

而不是单独removeFromSubview 为我要删除的每一件事,我想知道是否有什么东西可以从超级视图中删除所有内容

【问题讨论】:

    标签: ios cocoa-touch ios7


    【解决方案1】:

    我正在为此使用类别方法

    @interface UIView (Additions)
    - (void)removeAllSubviews
    @end
    
    @implementation UIView (Additions)
    - (void)removeAllSubviews
    {
        for (UIView *subview in self.subviews)
            [subview removeFromSuperview];
    }
    @end
    

    所以你可以打电话

    [self.superview removeAllSubviews];
    

    从超级视图中删除所有内容。

    【讨论】:

    • 您应该始终为类别方法使用前缀
    【解决方案2】:
    for(UIView *view in self.superview.subviews)
    {
        [view removeFromSuperview];
    }
    

    但请注意这一点,它可以删除您不知道的 Apple 内置插件,从而改变视图与用户交互的方式。

    【讨论】:

      【解决方案3】:

      一个班轮

      [viewYouWantToRemoveFrom.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 2012-12-17
        • 2018-03-01
        • 2022-01-23
        相关资源
        最近更新 更多