// // UIView+Find.m // // Created by MacKun on 15/4/25. // Copyright (c) 2015年 MacKun All rights reserved. // #import "UIView+Find.h" @implementation UIView (Find) /** * @brief 找到指定类名的view对象 * * @param clazz view类名 * * @return view对象 */ - (id)findSubViewWithSubViewClass:(Class)clazz { for (id subView in self.subviews) { if ([subView isKindOfClass:clazz]) { return subView; } } return nil; } /** * @brief 找到指定类名的SuperView对象 * * @param clazz SuperView类名 * * @return view对象 */ - (id)findSuperViewWithSuperViewClass:(Class)clazz { if (self == nil) { return nil; } else if (self.superview == nil) { return nil; } else if ([self.superview isKindOfClass:clazz]) { return self.superview; } else { return [self.superview findSuperViewWithSuperViewClass:clazz]; } } /** * @brief 找到并且resign第一响应者 * * @return 结果 */ - (BOOL)findAndResignFirstResponder { if (self.isFirstResponder) { [self resignFirstResponder]; return YES; } for (UIView *v in self.subviews) { if ([v findAndResignFirstResponder]) { return YES; } } return NO; } /** * @brief 找到第一响应者 * * @return 第一响应者 */ - (UIView *)findFirstResponder { if (([self isKindOfClass:[UITextField class]] || [self isKindOfClass:[UITextView class]]) && (self.isFirstResponder)) { return self; } for (UIView *v in self.subviews) { UIView *fv = [v findFirstResponder]; if (fv) { return fv; } } return nil; } @end