Question

In my iPad application I have an UIView with multiple subviews, each one with a different tag value. I want to search for a subview with specific tag value. Currently I am using the for() loop for looping through to find a specific UIView with the required tag.

 for (int i=0; i<[[myView subviews] count]; i++) {

    if ([[[myView subviews] objectAtIndex:1] tag]==1) {

        //assign that view

    }
}

I just want to know, whether [myView viewWithTag:1] also use same way to find that specific tagged view and which way is faster

Was it helpful?

Solution

Not exactly the same, viewWithTag uses depth-first search to find the subview. In another word, it scans all subviews recursively until it finds an UIView with a tag equals to the parameter.

See also in Apple official Documentation

OTHER TIPS

It is depending on your code. Suppose you create more number of subviews(like more than 10) dynamically you can use what you have written now. Suppose you know what the subviews are or you create less number of subviews(like less than 10) then you can use [myView viewWithTag:1] .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top