Skip to content

Instantly share code, notes, and snippets.

@aegzorz
Created July 11, 2013 10:40
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save aegzorz/5974444 to your computer and use it in GitHub Desktop.
Save aegzorz/5974444 to your computer and use it in GitHub Desktop.
Recursively find a 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