On iOS, is there a way to search ONLY subviews with a certain tag?

前端 未结 5 466
不知归路
不知归路 2021-01-21 16:55

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

5条回答
  •  梦毁少年i
    2021-01-21 17:16

    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)
    

提交回复
热议问题