从UIView到UIViewController?是否有一个内置的方法可以从UIView向其UIViewController?我知道你可以从UIViewController向其UIView通孔[self view]但我想知道是否有反向引用?
3 回答
繁星淼淼
TA贡献1775条经验 获得超11个赞
@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
// from a UIView subclass... returns nil if UIViewController not available UIViewController * myController = [self firstAvailableUIViewController];
隔江千里
TA贡献1906条经验 获得超10个赞
UIView
UIResponder
. UIResponder
-nextResponder
nil
. UIView
UIResponder
UIView
-nextResponder
@interface UIView (APIFix)- (UIViewController *)viewController;@end@implementation UIView (APIFix)- (UIViewController *)viewController { if ([self.nextResponder isKindOfClass:UIViewController.class]) return (UIViewController *)self.nextResponder; else return nil;}@end
UIView
- 3 回答
- 0 关注
- 398 浏览
添加回答
举报
0/150
提交
取消