我使用以下方法获取当前viewController
但它始终返回SWRevealViewController
作为currentController。
我使用SWRevealViewController
来处理幻灯片菜单。
extension UIApplication {
class func topViewController(base: UIViewController) -> UIViewController?{
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController!)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = base.presentedViewController {
return topViewController(presented)
}
return base
}
}
以下是我试图获取当前控制器的方法:
let topVC = UIApplication.topViewController(((UIApplication.sharedApplication().delegate as! AppDelegate).window?.rootViewController)!)
print(topVC)
if topVC!.isKindOfClass(mycontroller) == false{
//do something
}else{
// do another thing
}
}
答案 0 :(得分:1)
尝试像这样获取当前视图控制器:
UIViewController *topController = self.window.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}