首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIWindow函数在Xcode11.3中不起作用吗?

UIWindow函数在Xcode11.3中不起作用吗?
EN

Stack Overflow用户
提问于 2020-02-27 18:21:56
回答 2查看 42关注 0票数 0

我有像这样的alertcontroller控制器代码。

但我尝试显示警报,但警报没有显示给我。

对我来说有任何想法。

谢谢。

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

发布于 2020-02-27 19:02:37

似乎您需要保持UIWindow对象,直到您想要显示警报,这里是工作代码,只做了很小的更改

代码语言:javascript
复制
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
    }
}

使用

使用方法与您之前使用的方法相同

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2020-02-27 20:54:22

你也可以使用这个swift 5.0扩展

代码语言:javascript
复制
  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)
}
}

使用标题和消息设置警报控制器,并像这样调用

代码语言:javascript
复制
 alertObject.showAlert()
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60430960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档