Skip to content

Instantly share code, notes, and snippets.

@zbyhoo
Created October 6, 2011 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbyhoo/1266907 to your computer and use it in GitHub Desktop.
Save zbyhoo/1266907 to your computer and use it in GitHub Desktop.
Pretty nice way to find UIViewController of UIView using category
@interface UIView (FindUIViewController)
- (UIViewController *) firstAvailableUIViewController;
- (id) traverseResponderChainForUIViewController;
@end
@implementation UIView (FindUIViewController)
- (UIViewController *) firstAvailableUIViewController
{
// convenience function for casting and to "mask" the recursive function
return (UIViewController *)[self traverseResponderChainForUIViewController];
}
- (id) traverseResponderChainForUIViewController
{
id nextResponder = [self nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
return nextResponder;
else if ([nextResponder isKindOfClass:[UIView class]])
return [nextResponder traverseResponderChainForUIViewController];
else
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment