OC版本
id responder = self.nextResponder;while (![responder isKindOfClass: [UIViewController class]] && ![responder isKindOfClass: [UIWindow class]]) {
responder = [responder nextResponder];
}
if ([responder isKindOfClass: [UIViewController class]]) {
// responder就是view所在的控制器
// do something
}
Swift版本
func viewController(aClass: AnyClass) -> UIViewController?{
for(var next=self.superview;(next != nil);next=next?.superview){
let nextResponder = next?.nextResponder()
if((nextResponder?.isKindOfClass(aClass)) != nil){
return nextResponder as? UIViewController
}
}
return nil
}