【问题标题】:Cannot present UIAlertController in swift [duplicate]无法快速呈现 UIAlertController [重复]
【发布时间】:2020-08-25 05:30:59
【问题描述】:

我有这个功能

func showMessageNew(msg:String,title: String){
        let alertController = UIAlertController(title: title, message: msg, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default)
        alertController.addAction(okAction)
        self.present(alertController, animated: true)
}

但是当我运行它时,我在控制台中有这个:whose view is not in the window hierarchy!

【问题讨论】:

  • 为了显示警报,您需要一个父视图。你在哪里使用这个功能?
  • 你的函数在 viewDidLoad o viewAppear 中吗?

标签: ios swift uialertcontroller


【解决方案1】:

下面的代码可以用来呈现UIAlertController

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            self.showMessageNew(msg: "hi", title: "hi")
            }
        }

    func showMessageNew(msg:String,title: String){
        let alertController = UIAlertController(title: title, message: msg, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default)
        alertController.addAction(okAction)
        self.present(alertController, animated: true)
    }
}

【讨论】:

    猜你喜欢
    • 2015-01-13
    • 2020-10-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    相关资源
    最近更新 更多