tag是UIView的一個屬性,而且要求tag值唯一。父視圖可以通過tag來找到一個子視圖1 UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), ...
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值的唯一。