目的是為了寫一個簡易的show alert message的method
//程式碼
extension UIApplication {
class func topViewController(base base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
//用法
let presentedView = UIApplication.topViewController()
AlertMessageUtils.showSimpleAlert("系統通知", message: "電量\(values[0])%", view: presentedView!)
參考︰Find the current top view controller for your iOS application https://gist.github.com/snikch/3661188
留言
張貼留言