在View上获取当前控制器

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

}

你可能感兴趣的:(在View上获取当前控制器)