iOS 获取UIView所在的VIewController

写程序的时候我们经常要封装代码,当你封装了一个UIView的子类里需要调用所在ViewController的方法用如下代码调取所在的VIewController

 

#pragma mark - 获取所在ViewController
- (UIViewController *)superViewController:(id)sender {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

 

posted @ 2016-04-15 10:20  友朋  阅读(568)  评论(0编辑  收藏  举报