A little note on Viewwithtag

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.