Skip to content

Instantly share code, notes, and snippets.

@huangping123
Forked from aegzorz/UIView+Recursion.h
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save huangping123/1ef96b4d2397647c53c9 to your computer and use it in GitHub Desktop.

Select an option

Save huangping123/1ef96b4d2397647c53c9 to your computer and use it in GitHub Desktop.
递归查找subview
#import <UIKit/UIKit.h>
@interface UIView (Recursion)
/**
Return YES from the block to recurse into the subview.
Set stop to YES to return the subview.
*/
- (UIView*)findViewRecursively:(BOOL(^)(UIView* subview, BOOL* stop))recurse;
@end
#import "UIView+Recursion.h"
@implementation UIView (Recursion)
- (UIView*)findViewRecursively:(BOOL(^)(UIView* subview, BOOL* stop))recurse
{
for( UIView* subview in self.subviews ) {
BOOL stop = NO;
if( recurse( subview, &stop ) ) {
return [subview findViewRecursively:recurse];
} else if( stop ) {
return subview;
}
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment