在iOS上,有没有办法只搜索带有某个标签的子视图? [英] On iOS, is there a way to search ONLY subviews with a certain tag?

查看:241
本文介绍了在iOS上,有没有办法只搜索带有某个标签的子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为现在, viewWithTag 实际上首先搜索自己,然后在整个子树中递归搜索所有子视图,以获得带有该标记的视图。

Because right now, viewWithTag actually search for itself first, and then all subviews recursively down the whole subtree, for a view with that tag.

但是,如果我将子视图的标签设置为100,101等,以后再查找标签100,但是当前视图的父级将当前视图的标签设置为100?然后 viewWithTag 将返回当前视图而不是任何子视图。

But what if I set the tags of the subviews to 100, 101, etc, and later on, look for tag 100, but the parent of this current view sets the current view's tag to 100? Then viewWithTag will return the current view instead of any subview.

如果代码是

[fooView viewWithTag: 123]

为什么代码想要搜索包含fooView本身的子树?就像,代码不知道fooView足以想要搜索它。换句话说, fooView 被告知自己搜索......这很奇怪。一个观点不知道自己? (需要进行搜索才能找到自己?)

why would the code want to search the subtree including fooView itself? It is like, the code doesn't know fooView good enough to want to search for it too. Or put it another way, fooView is told to search itself... which is strange. A view doesn't know itself? (need to do a search to look for itself?)

那么有没有办法只搜索子视图和宏观子视图(不搜索自己)?

So is there a way to search for subviews and grand-subviews only (without searching for self)?

推荐答案

利用 -viewWithTag的递归特性:

- (UIView *)viewWithTagNotCountingSelf:(NSInteger)tag
{
    UIView *toReturn = nil;

    for (UIView *subView in self.subviews) {
        toReturn = [subView viewWithTag:tag];

        if (toReturn) {
            break;
        }
    }
    return toReturn;
}

编辑:这将比grand-subviews向下钻取:它将会获取非自身层次结构中的任何视图。这也是在 UIView 的类别中实现的。

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.

这篇关于在iOS上,有没有办法只搜索带有某个标签的子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆