Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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