extension UIViewController {
static var current:UIViewController? {
let delegate = UIApplication.shared.delegate as? AppDelegate
var current = delegate?.window.rootViewController
while (current?.presentedViewController != nil) {
current = current?.presentedViewController
}
if let tabbar = current as? UITabBarController , tabbar.selectedViewController != nil {
current = tabbar.selectedViewController
}
while let navi = current as? UINavigationController , navi.topViewController != nil {
current = navi.topViewController
}
return current
}
}
调用的话 直接 UIViewcontroller.current