天天看点

viewWithTag简单用法 寻找视图中特定的子视图

1.原理在创建控件时候设置特定的tag ,viewWithTag 会从self.view 逐级往下搜索找到对应的tag 并返回控件对应的类型 ,搜索不到返回nil。

创建view

for (int i = 0; i < 100; i++)

{

//row and column

//行

int row = i/3;

//列

int colume = i%3;

//创建子视图

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRECTMake(10+colume*100,10+row*100,90,90)];

imageView.Tag = 100+i;

[self.vew addSubview:imageView];

}

根据tag移除view

for (int i=0; i < 100; i++)

{

//根据tag找到对应的view

UIImageView *imgView = [self.view viewWithTag:100+i];

//移除对应的view

[imgView removeFromSuperview];

}

继续阅读