如何从UIView 得到 UIViewController

参考 :http://stackoverflow.com/questions/2309569/how-to-get-uiviewcontroller-of-a-uiviews-superview-in-iphone-sdk



@implementation UIView (KalAdditions)

//得到此view 所在的viewController

- (UIViewController *)viewController;



- (UIViewController*)viewController {

    for (UIView* next = [self superview]; next; next = next.superview) {

        UIResponder* nextResponder = [next nextResponder];

        if ([nextResponder isKindOfClass:[UIViewController class]]) {

            return (UIViewController*)nextResponder;

        }

    }

    return nil;

}


你可能感兴趣的:(如何从UIView 得到 UIViewController)