Advertisement
kambala

iOS: recursively find subview matching condition

Aug 26th, 2022
2,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. + (UIView *)viewOfClass:(NSString *)classStr insideView:(UIView *)view withCondition:(ViewTestBlock)viewTestBlock {
  2.     return [self viewOfClass:classStr insideView:view shouldMatchClassStrictly:NO withCondition:viewTestBlock];
  3. }
  4.  
  5. + (UIView *)viewOfClass:(NSString *)classStr insideView:(UIView *)view shouldMatchClassStrictly:(BOOL)shouldMatchClassStrictly withCondition:(ViewTestBlock)viewTestBlock {
  6.     Class class = NSClassFromString(classStr);
  7.     for (UIView *v in view.subviews) {
  8.         UIView *result;
  9.         if (shouldMatchClassStrictly ? [v isMemberOfClass:class] : [v isKindOfClass:class]) {
  10.             if (!viewTestBlock || viewTestBlock(v)) {
  11.                 result = v;
  12.             }
  13.         }
  14.         else {
  15.             result = [self viewOfClass:classStr insideView:v shouldMatchClassStrictly:shouldMatchClassStrictly withCondition:viewTestBlock];
  16.         }
  17.         if (result) {
  18.             return result;
  19.         }
  20.     }
  21.     return nil;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement