Obtain subview
Generally, we can perform subview operations at the view level in two ways: 1. retain a subview reference, and then perform operations on the subview through the reference in the class, however, pay attention to the addition of memory maintenance in the appropriate locationCode, Manually released before exiting. 2. Set the subview tag so that the corresponding subview can be obtained through viewwithtag when the subview is to be used. This method is concise and does not need to be maintained by yourself.
Viewwithtag: usually adopts the depth traversal priority.AlgorithmReturns the subview with the first tag equal to the given tag. This leads to the fact that when the tags of multiple subviews In a view are the same, the view we get through this method may not be what we want.
The code below is verified through a small example:
Svtestviewtag. h
//
// Svtestviewtag. h
// Svuiviewsample
//
// Created by MAPLE on 3/18/12.
// Copyright (c) 2012 smileevday. All rights reserved.
//
// When a view obtains a subview based on a tag, it executes the depth-first traversal algorithm.
// Returns the child view with the first tag equal to the request tag.
// Search from subviews, and find the lowest level first
# Import <Uikit/uikit. h>
@ Interface Svtestviewwithtag: uiview
@ End
Svtestviewwithtag. m
The hierarchy chart of the view created by the code in the example is as follows:
In this example, each subview is a uilabel and corresponding content is set. Implementation of the response function of the button: first, hide all subviews of the uilabel type (exclude uibutton because the button needs to be displayed all the time), and then obtain the corresponding subview Based on the specified tag, the hidden attribute of the subview and its superview is no. This ensures that only the subview with the first tag and the specified tag are displayed when you click the button.
To verify how viewwithtag obtains the subview:
First, I added subview11 and subview12 Whose tags are both 11 in subview1. RunProgramYes. When we click "show tag 11", "subview11" is displayed instead of "subview12 ". At the same time, no matter how many times you click this button, only "subview11" is displayed ". In this way, we can see that the search order for the subview obtained at the same level is from small to large, that is, the lower level will be first found.
Next, I also added subview13 with 13 tags in subview1, added subview2 with 13 tags in view, and clicked "show tag 13" to run the program, "subview13" is displayed instead of "subview2 ". This verifies that viewwithtag follows the depth-first traversal principle when searching, that is, it first searches for the bottom view and recursively queries its subview.
To sum up the above two points, we can see the basic principle of obtaining subview by viewwithtag, that is, follow the two principles of depth first and lower layer first.
PS: During the test, I also found that if I click show tag 13 and then click show tag 11, the effect will be lost. I don't know why, however, restarting the program will return to normal.
Looking forward to your answers!