• iOS 遍历所有子视图subviews的2种方法


    在做项目的过程中,难免有时候会需要利用父子视图的关系去调用一些behavior。

    方法一,比较直接

        for (UIView *subViews in yourView.subviews) {  
            [self removSubviews:subViews];  
        }     
          
        -(void)removSubviews:(UIView *)subView  
        {  
          if (subView.subviews.count>0) {  
            for (UIView *subViews in subView.subviews) {  
    
               [self removSubviews:subViews];  
            }  
          }  
          else  
          {  
             NSLog(@"%i",subView.subviews.count);  
            [subView removeFromSuperview];  
          }  
        }  

    方法二,用类别

    // UIView+HierarchyLogging.h  
    @interface UIView (ViewHierarchyLogging)  
    - (void)logViewHierarchy;  
    @end  
      
    // UIView+HierarchyLogging.m  
    @implementation UIView (ViewHierarchyLogging)  
    - (void)logViewHierarchy  
    {  
        NSLog(@"%@", self);  
        for (UIView *subview in self.subviews)  
        {  
            [subview logViewHierarchy];  
        }  
    }  
    @end  
      
    // In your implementation  
    [myView logViewHierarchy];  
  • 相关阅读:
    LeetCode
    LeetCode
    控制反转(Ioc)
    KMP算法
    *&m与m的区别
    函数指针与函数指针数组的使用方法
    C++四种类型转换
    内存分配:堆内存,栈内存
    汇编 基础
    i++,++i 作为参数
  • 原文地址:https://www.cnblogs.com/allanliu/p/4229762.html
Copyright © 2020-2023  润新知