Tag learning for IOS UIView, iosuiviewtag
Tag is an attribute of UIView, And the tag value must be unique. A parent view can be found using tags.
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 usage
The following are several Error Examples that need attention.
Because there are a lot of sample code, you can jump to the final conclusion section (it is best to take a look at the code in error Example 4)
1. Two subviews with the same tag value under the view
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];Error Example 1
The result is that the background of the yellowView is changed. In this case, the first sub-view with the tag 1000 loaded by the parent view is obtained.
2. The parent view gets the child view of the Child view.
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];Example 2
The result is that the greenView background is changed, which indicates that this is feasible.
3. Take a view that exists but is not a subview of the current user.
Change the code in the penultimate sentence in example 2
UIView *getView = [redView viewWithTag:1003];
The result is that the background of greenView is not changed, which means this is not feasible.
4. The sub-views in other views have the same tag value as the sub-views in this view.
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];Error Example 3
The result is OK.
5. The tag values of greenView and redView are the same.
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];Error Example 4
The result isThe background color of greenView is changed..
Conclusion:
The code above can be introduced. The order in which the parent view obtains sub-views through tags isDepth first, That is, first view your first sub-view, and then viewAll subviews of the first subviewOf
Tag value until all sub-Views under the first sub-view are searched,Search for your second sub-View,Until it is found. Nil is returned if it cannot be found..
Of course, the most secure method is to ensure the tag value.Unique.