[英]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.