您的位置:首页 > 其它

视图关系及遍历查找视图

2015-02-26 20:13 127 查看
// 会将响应者链上的所有子视图统统移除
--------------------

[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];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  遍历 响应