视图关系及遍历查找视图

来源:互联网 发布:怎样办理淘宝3c认证 编辑:程序博客网 时间:2024/06/07 10:13
// 会将响应者链上的所有子视图统统移除--------------------    [tv1 removeFromSuperview];// 循环创建10个子视图 v1加在v2上 v2加在v3上 .... 随机颜色 子视图比父视图宽高各小20个像素--------------------------------------------------------    UIView * preView = nil;    for (int i = 0; i < 10; i++) {        UIView * v = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 320-20*i, 260-20*i)];        // 生成随机色        int r = arc4random() % 256;        int g = arc4random() % 256;        int b = arc4random() % 256;        v.backgroundColor = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0];        // 第一个视图        if (preView == nil) {            v.frame = CGRectMake(0, 220, 320, 260);            [self.window addSubview:v];        }else{            [preView addSubview:v];        }        // 保存上一级父视图对象        preView = v;    }    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 30)];    label.text = @"Test";    [self.window addSubview:label];    NSLog(@">>>%@",self.window.subviews);// 遍历查找子视图 查找一种类型的视图 UIButton UILabel...----------------------------------------    for (UIView * v in self.window.subviews) {        // v对象的类型是 UILabel        if ([v isKindOfClass:[UILabel class]]) {            UILabel * l = (UILabel *)v;            l.textColor = [UIColor redColor];        }    }
0 0
原创粉丝点击