简体   繁体   中英

Does UIWindow function not work in Xcode11.3?

I have alertcontroller code like this.
But I try to present alert, the alert don't show to me.
Have any idea to me.
Thanks.

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()

Seems you need to hold UIWindow object until you want to show alert Here is working code with little changes

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
    }
}

usage
Use in same way as you were previously using


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()

You can use this swift 5.0 extension also

  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)
}
}

Setup your alert controller with title and message and call like this

 alertObject.showAlert()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM