Last active
December 17, 2015 01:39
-
-
Save chrislavender/5530269 to your computer and use it in GitHub Desktop.
Find the ViewController that contains a UIView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/questions/1340434/get-to-uiviewcontroller-from-uiview-on-iphone | |
@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