IOS之UIView的tag学习

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

1     UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/2)];
2     redView.backgroundColor = [UIColor redColor];
3     redView.tag = 1000;
4 [self.window addSubview:redView];
5 
6 UIView *getView = [self.window viewWithTag:1000];
tag用法

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

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

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

 1     UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/2)];
 2     redView.backgroundColor = [UIColor redColor];
 3     redView.tag = 1000;
 4     
 5     UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.window.frame)/2, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/2)];
 6     yellowView.backgroundColor = [UIColor yellowColor];
 7     yellowView.tag = 1000;
 8 
 9     [self.window addSubview:yellowView];
10     [self.window addSubview:redView];
11     
12     UIView *getView = [self.window viewWithTag:1000];
13     [getView setBackgroundColor:[UIColor whiteColor]];
错误示例1

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

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

 1     UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/2)];
 2     redView.backgroundColor = [UIColor redColor];
 3     redView.tag = 1000;
 4     
 5     UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.window.frame)/2, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/2)];
 6     yellowView.backgroundColor = [UIColor yellowColor];
 7     yellowView.tag = 1001;
 8     
 9     UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
10     blueView.backgroundColor = [UIColor blueColor];
11     blueView.tag = 1002;
12     
13     UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
14     greenView.backgroundColor = [UIColor greenColor];
15     greenView.tag = 1003;
16     
17     [redView addSubview:blueView];
18     [yellowView addSubview:greenView];
19     
20     
21     [self.window addSubview:yellowView];
22     [self.window addSubview:redView];
23     
24     UIView *getView = [self.window viewWithTag:1003];
25     [getView setBackgroundColor:[UIColor whiteColor]];
示例2

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

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

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

UIView *getView = [redView viewWithTag:1003];

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

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

 1     UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/2)];
 2     redView.backgroundColor = [UIColor redColor];
 3     redView.tag = 1000;
 4     
 5     UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.window.frame)/2, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/2)];
 6     yellowView.backgroundColor = [UIColor yellowColor];
 7     yellowView.tag = 1001;
 8     
 9     UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
10     blueView.backgroundColor = [UIColor blueColor];
11     blueView.tag = 1002;
12     
13     UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
14     greenView.backgroundColor = [UIColor greenColor];
15     greenView.tag = 1002;
16     
17     [redView addSubview:blueView];
18     [yellowView addSubview:greenView];
19     
20     
21     [self.window addSubview:yellowView];
22     [self.window addSubview:redView];
23     
24     UIView *getView = [redView viewWithTag:1002];
25     [getView setBackgroundColor:[UIColor whiteColor]];
错误示例3

结果来看还可以

五,greenView和redView的tag值相同

 1     UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/2)];
 2     redView.backgroundColor = [UIColor redColor];
 3     redView.tag = 1000;
 4     
 5     UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.window.frame)/2, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/2)];
 6     yellowView.backgroundColor = [UIColor yellowColor];
 7     yellowView.tag = 1001;
 8     
 9     UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
10     blueView.backgroundColor = [UIColor blueColor];
11     blueView.tag = 1002;
12     
13     UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
14     greenView.backgroundColor = [UIColor greenColor];
15     greenView.tag = 1000;
16     
17     [redView addSubview:blueView];
18     [yellowView addSubview:greenView];
19     
20     
21     [self.window addSubview:yellowView];
22     [self.window addSubview:redView];
23     
24     UIView *getView = [self.window viewWithTag:1000];
25     [getView setBackgroundColor:[UIColor whiteColor]];
错误示例4

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

最后说一下结论

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

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

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

原文地址:https://www.cnblogs.com/ieso-ios/p/5114416.html