UIView+Find.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // UIView+Find.m
  3. //
  4. // Created by MacKun on 15/4/25.
  5. // Copyright (c) 2015年 MacKun All rights reserved.
  6. //
  7. #import "UIView+Find.h"
  8. @implementation UIView (Find)
  9. /**
  10. * @brief 找到指定类名的view对象
  11. *
  12. * @param clazz view类名
  13. *
  14. * @return view对象
  15. */
  16. - (id)findSubViewWithSubViewClass:(Class)clazz
  17. {
  18. for (id subView in self.subviews) {
  19. if ([subView isKindOfClass:clazz]) {
  20. return subView;
  21. }
  22. }
  23. return nil;
  24. }
  25. /**
  26. * @brief 找到指定类名的SuperView对象
  27. *
  28. * @param clazz SuperView类名
  29. *
  30. * @return view对象
  31. */
  32. - (id)findSuperViewWithSuperViewClass:(Class)clazz
  33. {
  34. if (self == nil) {
  35. return nil;
  36. } else if (self.superview == nil) {
  37. return nil;
  38. } else if ([self.superview isKindOfClass:clazz]) {
  39. return self.superview;
  40. } else {
  41. return [self.superview findSuperViewWithSuperViewClass:clazz];
  42. }
  43. }
  44. /**
  45. * @brief 找到并且resign第一响应者
  46. *
  47. * @return 结果
  48. */
  49. - (BOOL)findAndResignFirstResponder {
  50. if (self.isFirstResponder) {
  51. [self resignFirstResponder];
  52. return YES;
  53. }
  54. for (UIView *v in self.subviews) {
  55. if ([v findAndResignFirstResponder]) {
  56. return YES;
  57. }
  58. }
  59. return NO;
  60. }
  61. /**
  62. * @brief 找到第一响应者
  63. *
  64. * @return 第一响应者
  65. */
  66. - (UIView *)findFirstResponder {
  67. if (([self isKindOfClass:[UITextField class]] || [self isKindOfClass:[UITextView class]])
  68. && (self.isFirstResponder)) {
  69. return self;
  70. }
  71. for (UIView *v in self.subviews) {
  72. UIView *fv = [v findFirstResponder];
  73. if (fv) {
  74. return fv;
  75. }
  76. }
  77. return nil;
  78. }
  79. @end