Usually we use Viewwithtag for the following scenarios:
If we use a parent view with multiple sub-view, each sub-view is marked with a tag value starting at 0, so that the Viewwithtag:tag value can be used to fetch each sub-view directly on the image view.
The view that was taken with [parent view viewwithtag:0] is not a child view, but the parent view,
It was later recalled that the Apple document mentioned once, the tag value is smaller, such as 0-100 for Apple reserved for use, and 0 is reserved for themselves this view use.
For other view, such as ScrollView, the above 0 may be a reserved value.
So when using Viewwithtag, and when setting the tag value of the sub-view, you need to be careful not to use the lower value of the tag value, when using a fixed value
#define TILEINITIALTAG 10000
When used, the following
Curtileview_0.tag = Tileinitialtag + emptyplaceindex_0;
This can be effectively avoided because the tag value is too small to take to the system reserved view.
Statement two:
Someone used the Viewwithtag method to find a tag problem and then made a small test:
UIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake (0, 0, 50, 50)];
Btn.backgroundcolor = [Uicolor blackcolor];
Btn.tag = 0;
[Self.mview ADDSUBVIEW:BTN];
[BTN release];
[Self.mview viewwithtag:0].backgroundcolor = [Uicolor Whitecolor];
The result is that the self.mview background color turns white, and the debug found that the Self.mview tag is also 0. Find document Discovery The default tag value for UIView is 0. and [Self.mview viewwithtag:0] takes precedence over the tag of the function caller itself, and if the function caller meets the criteria it returns the function caller itself, so it cannot find the btn.
Now change the code.
UIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake (0, 0, 50, 50)];
Btn.backgroundcolor = [Uicolor blackcolor];
Btn.tag = 0;
Self.mView.tag = 1;
[Self.mview ADDSUBVIEW:BTN];
[BTN release];
[Self.mview viewwithtag:0].backgroundcolor = [Uicolor Whitecolor];
The result is that BTN's background color turns white, which means Viewwithtag found btn.
Thanks for sharing
A little note on Viewwithtag