Функция UIWindow не работает в Xcode11.3? - PullRequest
0 голосов
/ 27 февраля 2020

У меня есть код контроллера оповещений, подобный этому.
Но я пытаюсь представить оповещение, предупреждение не показывается мне.
Есть идеи для меня.
Спасибо.

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

Ответы [ 2 ]

2 голосов
/ 27 февраля 2020

Кажется, вам нужно держать объект 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()
0 голосов
/ 27 февраля 2020

Вы также можете использовать это расширение 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()
...