官方解释

UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t); UIViewController implements the method by returning its view’s superview; UIWindow returns the application object, and UIApplication returns nil.

id next = [self nextResponder];
while(![next isKindOfClass:[ViewController class]]) {
    next = [next nextResponder];
}
if ([next isKindOfClass:[ViewController class]]) {
    controller = (ViewController *)next;
}