// // UIViewController+Extension.swift // zc // // Created by wyp on 2020/10/22. // Copyright © 2020 wyp. All rights reserved. // import UIKit public extension UIViewController { func showAlert(title: String, message: String, confirm: ((UIAlertAction) -> Void)? = nil) { showAlert(title: title, message: message, confirm: confirm, cancel: nil) } func showAlert(title: String, message: String, confirmText: String = "确定", cancelText: String? = "取消", confirm: ((UIAlertAction) -> Void)? = nil, cancel: ((UIAlertAction) -> Void)? = nil) { var conText = confirmText var canText = cancelText if confirmText == "确定" { conText = LocString("确定") } if cancelText == "取消" { canText = LocString("取消") } let alertVC = UIAlertController(title: title, message: message, preferredStyle: .alert) if canText != nil { let cancelAction = UIAlertAction(title: canText, style: .default, handler: cancel) cancelAction.setValue(UIColor.rgbColorFromHex(0x808080), forKey: "titleTextColor") alertVC.addAction(cancelAction) } let confirmAction = UIAlertAction(title: conText, style: .destructive, handler: confirm) confirmAction.setValue(UIColor.rgbColorFromHex(0x00993E), forKey: "titleTextColor") alertVC.addAction(confirmAction) present(alertVC, animated: true, completion: nil) } class func getCurrentViewController() -> UIViewController?{ // 获取当先显示的window var currentWindow = UIApplication.shared.keyWindow ?? UIWindow() if currentWindow.windowLevel != UIWindow.Level.normal { let windowArr = UIApplication.shared.windows for window in windowArr { if window.windowLevel == UIWindow.Level.normal { currentWindow = window break } } } return UIViewController.getNextXController(nextController: currentWindow.rootViewController) } private class func getNextXController(nextController: UIViewController?) -> UIViewController? { if nextController == nil { return nil } else if nextController?.presentedViewController != nil { return UIViewController.getNextXController(nextController: nextController?.presentedViewController) } else if let tabbar = nextController as? UITabBarController { return UIViewController.getNextXController(nextController: tabbar.selectedViewController) } else if let nav = nextController as? UINavigationController { return UIViewController.getNextXController(nextController: nav.visibleViewController) } return nextController } }