Because right now, viewWithTag
actually search for itself first, and then all subviews recursively down the whole subtree, for a view with that tag.
But wha
let result = view.subviews.filter{$0.tag == tag}.first
For 1 level:
UIView *view;
for (int i = 0; i < viewToSearch.subviews.count; i++){
UIView *subview = viewToSearch.subviews[i];
if (subview.tag == tagToSeach){
view = subview;
break;
}
}
To search a view hierarchy with multiple levels:
__block UIView *view;
BOOL (^__block searchViewForTag)(UIView *,NSInteger) = ^(UIView *aView, NSInteger tag){
for (UIView *subview in aView.subviews){
if (subview.tag == tag){
view = subview;
return YES;
}
if (searchViewForTag(subview,tag)) return YES;
}
return NO;
};
NSInteger tagToSearchFor = 1;
searchViewForTag(viewToSearch,tagToSearchFor);
//Do something with view
Take advantage of the recursive nature of -viewWithTag:
- (UIView *)viewWithTagNotCountingSelf:(NSInteger)tag
{
UIView *toReturn = nil;
for (UIView *subView in self.subviews) {
toReturn = [subView viewWithTag:tag];
if (toReturn) {
break;
}
}
return toReturn;
}
Edit: this will drill down farther than "grand-subviews": it will get any view within the hierarchy that is not self. Also this is to be implemented in a category on UIView
.
After reviewing the documentation for -viewWithTag:
and running a few tests, it appears the answer to OP's question is - This behavior is already provided.
Return Value
The view in the receiver’s hierarchy whose tag property matches the value in the tag parameter.
Discussion
This method searches the current view and all of its subviews for the specified view.
I am concluding this to mean that 'view' is also a 'subview', thus limiting the scope of the search.
do this:
NSMutableArray *arrSameViewTag = [NSMutableArray array];
for(UIView *subview in [yourView subviews]) //your view to find subview
{
if(subview.tag == 123) //specific tah here
{
[arrSameViewTag addObject:subview]; //view found add in array
}
}
NSlog(@"arrSameViewTag : %@",arrSameViewTag);
To find specific like UIButton or any UIElement then like this:
NSMutableArray *arrSameViewTag = [NSMutableArray array];
for(id *subview in [yourView subviews]) //your view to find subview
{
if([subview isKindofClass[UIButton class]) //any UIElement of specific type here
{
UIButton *btn = (UIButton *)subview; //same UIElement mentioned for checking it
if(btn.tag == 123) //specific tah here
{
[arrSameViewTag addObject:subview]; //view found add in array
}
}
}
NSlog(@"arrSameViewTag : %@",arrSameViewTag)