如何使用委托 iOS swift 在当前视图中显示警报?
How to show the alert in current view using delegates iOS swift?
我们必须在后台线程中将图像上传到 Web 服务。例如,我有一个主屏幕和登录屏幕。当我在主屏幕中上传图像并导航到登录屏幕时,如果在主屏幕中出现超时错误,我必须在登录屏幕中显示警报。
注意:我必须在当前视图中调用后台线程调用委托方法来显示警报,而不是在 window root[=13 中使用 UIAlertController =]
在所有视图之上显示警报:
func alertWindow(title: String, message: String) {
DispatchQueue.main.async(execute: {
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert + 1
let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction2 = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert2.addAction(defaultAction2)
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alert2, animated: true, completion: nil)
}) }
Function calling:::::
SharedClass.sharedInstance.alertWindow(title:"This your title", message:"This is your message")
上面的代码工作正常,但我必须使用委托在当前视图中显示警报,该怎么做?
使用此方法获取视图层次结构中最顶层的控制器
func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
并这样称呼它:
topViewController()?.present(yourvc, animated: true, completion: nil)
我们必须在后台线程中将图像上传到 Web 服务。例如,我有一个主屏幕和登录屏幕。当我在主屏幕中上传图像并导航到登录屏幕时,如果在主屏幕中出现超时错误,我必须在登录屏幕中显示警报。
注意:我必须在当前视图中调用后台线程调用委托方法来显示警报,而不是在 window root[=13 中使用 UIAlertController =]
在所有视图之上显示警报:
func alertWindow(title: String, message: String) {
DispatchQueue.main.async(execute: {
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert + 1
let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction2 = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert2.addAction(defaultAction2)
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alert2, animated: true, completion: nil)
}) }
Function calling:::::
SharedClass.sharedInstance.alertWindow(title:"This your title", message:"This is your message")
上面的代码工作正常,但我必须使用委托在当前视图中显示警报,该怎么做?
使用此方法获取视图层次结构中最顶层的控制器
func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
并这样称呼它:
topViewController()?.present(yourvc, animated: true, completion: nil)