Sometimes it's needed to check whether a UIView is a subview of another view. A typical use case is show and hide UIViews with button click events. it's better if the code check whether a UIView is a sub view of another view before the view is removed from the super view. UIView class have method "isDescendantOfView" [1] it returns "YES" if the specified UIView is a subview of another UIView if it's not it returns "NO".
In example code below, "I m in" is printed if the theSubView is a sub view of the UIView self.view otherwise it prints "I m out".
if ([theSubView isDescendantOfView:self.view]) {
NSLog(@"I m in");
}else{
NSLog(@"I m out");
}
Reference:[1]. http://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/isDescendantOfView:
No comments:
Post a Comment