IOS之UIView的tag学习

时间:2023-03-09 12:54:26
IOS之UIView的tag学习

tag是UIView的一个属性,而且要求tag值唯一。父视图可以通过tag来找到一个子视图

     UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
redView.backgroundColor = [UIColor redColor];
redView.tag = ;
[self.window addSubview:redView]; UIView *getView = [self.window viewWithTag:];

tag用法

下面来看下几种需要注意的错误示例

由于示例代码有点多,怕麻烦的童鞋可以跳到最后的结论部分(最好把错误示例4的代码看一下)

一,view下有tag值相同的两个subview

     UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
redView.backgroundColor = [UIColor redColor];
redView.tag = ; UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(, CGRectGetHeight(self.window.frame)/, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.tag = ; [self.window addSubview:yellowView];
[self.window addSubview:redView]; UIView *getView = [self.window viewWithTag:];
[getView setBackgroundColor:[UIColor whiteColor]];

错误示例1

结果是yellowView的背景被改变了,说明这种情况下会取得父视图加载的第一个tag是1000的子视图。

二,父视图取得子视图的子视图。

     UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
redView.backgroundColor = [UIColor redColor];
redView.tag = ; UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(, CGRectGetHeight(self.window.frame)/, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.tag = ; UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
blueView.backgroundColor = [UIColor blueColor];
blueView.tag = ; UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
greenView.backgroundColor = [UIColor greenColor];
greenView.tag = ; [redView addSubview:blueView];
[yellowView addSubview:greenView]; [self.window addSubview:yellowView];
[self.window addSubview:redView]; UIView *getView = [self.window viewWithTag:];
[getView setBackgroundColor:[UIColor whiteColor]];

示例2

结果是greenView背景被改变了,说明这个是可行的。

三,取存在但不是自己子视图的视图

将示例2中的倒数第二句代码改为

UIView *getView = [redView viewWithTag:];

结果是greenView的背景没有被改变,说明这是行不通的。

四,别的视图中的子视图和本视图的子视图有相同的tag值

     UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
redView.backgroundColor = [UIColor redColor];
redView.tag = ; UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(, CGRectGetHeight(self.window.frame)/, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.tag = ; UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
blueView.backgroundColor = [UIColor blueColor];
blueView.tag = ; UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
greenView.backgroundColor = [UIColor greenColor];
greenView.tag = ; [redView addSubview:blueView];
[yellowView addSubview:greenView]; [self.window addSubview:yellowView];
[self.window addSubview:redView]; UIView *getView = [redView viewWithTag:];
[getView setBackgroundColor:[UIColor whiteColor]];

错误示例3

结果来看还可以

五,greenView和redView的tag值相同

     UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
redView.backgroundColor = [UIColor redColor];
redView.tag = ; UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(, CGRectGetHeight(self.window.frame)/, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.tag = ; UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
blueView.backgroundColor = [UIColor blueColor];
blueView.tag = ; UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
greenView.backgroundColor = [UIColor greenColor];
greenView.tag = ; [redView addSubview:blueView];
[yellowView addSubview:greenView]; [self.window addSubview:yellowView];
[self.window addSubview:redView]; UIView *getView = [self.window viewWithTag:];
[getView setBackgroundColor:[UIColor whiteColor]];

错误示例4

结果是greenView的背景色被改变了

最后说一下结论

由以上的代码可以推出,父视图通过tag取得子视图的顺序是深度优先,也就是先查看自己的第一个子视图,然后查看第一个子视图的所有子视图

tag值,直到第一个子视图下的所有子视图搜索完,再搜索自己第二个子视图直到找到为止。找不到就返回nil

当然,最稳妥的方法还是确保tag值的唯一