[英]Does UIWindow function not work in Xcode11.3?
我有这样的警报控制器代码。
但我尝试呈现警报,警报不显示给我。
对我有什么想法。
谢谢。
public extension UIAlertController {
func show() {
let win = UIWindow(frame: UIScreen.main.bounds)
let vc = UIViewController()
vc.view.backgroundColor = .clear
win.rootViewController = vc
win.windowLevel = UIWindow.Level.alert + 1
win.makeKeyAndVisible()
vc.present(self, animated: true, completion: nil)
}
}
let alertController = UIAlertController(title: newTitle, message: newMessage, preferredStyle: .alert)
let submit = UIAlertAction(title: submitTitle, style: .default) { (action) in
clickOK()
}
alertController.addAction(submit)
if let cancelTitle = cancelTitle, !cancelTitle.isEmpty {
let cancel = UIAlertAction(title: cancelTitle, style: .cancel) { (action) in
if let clickCancel = clickCancel {
clickCancel()
}
}
alertController.addAction(cancel)
}
alertController.show()
似乎你需要持有 UIWindow 对象,直到你想显示警报这是工作代码,几乎没有变化
private var win: UIWindow!
extension UIAlertController {
func show() {
win = UIWindow(frame: UIScreen.main.bounds)
let vc = UIViewController()
vc.view.backgroundColor = .clear
win.rootViewController = vc
win.windowLevel = .alert + 1
win.makeKeyAndVisible()
win.rootViewController?.present(self, animated: true, completion: nil)
}
open override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
win = nil
}
}
用法
使用方式与您之前使用的相同
let alertController = UIAlertController(title: newTitle, message: newMessage, preferredStyle: .alert)
let submit = UIAlertAction(title: submitTitle, style: .default) { (action) in
clickOK()
}
alertController.addAction(submit)
if let cancelTitle = cancelTitle, !cancelTitle.isEmpty {
let cancel = UIAlertAction(title: cancelTitle, style: .cancel) { (action) in
if let clickCancel = clickCancel {
clickCancel()
}
}
alertController.addAction(cancel)
}
alertController.show()
你也可以使用这个 swift 5.0 扩展
public extension UIAlertController {
func showAlert() {
let window = UIWindow(frame: UIScreen.main.bounds)
let vc = UIViewController()
vc.view.backgroundColor = .clear
window.rootViewController = vc
window.windowLevel = UIWindow.Level.alert + 1
window.makeKeyAndVisible()
vc.present(self, animated: true, completion: nil)
}
}
使用标题和消息设置警报控制器并像这样调用
alertObject.showAlert()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.