我的 ViewController 中有一堆 subview 。
在最后一层我有一个 UIView,从这个 View 我想调用 superview 并向上直到找到属于我的 ViewController 的 UIView。
是否可以判断一个 UIView 是否属于一个 ViewController?
UIView *someView = self.superView;
while (true)
{
if (someView BELONGS TO VIEWCONTROLLER)
{
// Now we know this view belongs to a VIewController
break;
}
someView = someView.superView;
}
最佳答案
如果你想知道某个 View 是否在 View Controller 管理的层次结构中,并且你有一个指向 View Controller 的指针:
BOOL belongsToController = [aView isDescendantOfView:viewController.view];
或者,如果您想知道某个 View 是否是 View Controller 管理的层次结构的根,但您没有指向 View Controller 的指针,您可以遍历响应者链。根据 UIResponder
的 nextResponder文档:
UIView
implements this method by returning theUIViewController
object that manages it (if it has one) or its superview (if it doesn’t)
因此,如果某个 View 的下一个响应者是 UIViewController
,则该 View 必须是与 View Controller 关联的 View 。
if ([[aView nextResponder] isKindOfClass:[UIViewController class]]) {
// aView is the root of the view hierarchy managed by the view controller
}
关于iphone - iOS - UIView 是否属于 UIViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7035927/