不显示UIAlertController。

问题描述 投票:0回答:1

这是我 ViewController.swift:

import Intents

class ViewController: UIViewController {

    @IBAction func getdbb() {
            db().getdb { (Db) in
                let alert1 = UIAlertController (title: "Random Trivia", message : Db,  preferredStyle: .alert)
                alert1.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
                    let alert2 = UIAlertController(title: nil,  message: "Do you want to go to the next question?", preferredStyle: .alert)
                    alert2.addAction(UIAlertAction(title: "YES", style: .default, handler:{ action in self.getdbb() }))
                    alert2.addAction(UIAlertAction(title :"NO", style: .default, handler: nil ))

                }))
                self.present(alert1, animated: true, completion: nil)
                INInteraction(intent: GetQuestionIntent(), response: nil).donate(completion: nil)

        }

    }

}

在我的main.storyboard中,我有一个查询按钮,它连接到了 getdbb 函数,但在模拟器中却没有显示警报UI。当我点击查询按钮时,就会发生这样的情况(输出只在Xcode中显示)。

enter image description here

ios swift mobile uialertview
1个回答
-1
投票

你没有在主线程中展示控制器,所以可以试试下面的方法。

db().getdb { (Db) in

    DispatchQueue.main.async {
                let alert1 = UIAlertController (title: "Random Trivia", message : Db,  preferredStyle: .alert)
                alert1.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
                    let alert2 = UIAlertController(title: nil,  message: "Do you want to go to the next question?", preferredStyle: .alert)
                    alert2.addAction(UIAlertAction(title: "YES", style: .default, handler:{ action in self.getdbb() }))
                    alert2.addAction(UIAlertAction(title :"NO", style: .default, handler: nil ))

                }))
                self.present(alert1, animated: true, completion: nil)
                INInteraction(intent: GetQuestionIntent(), response: nil).donate(completion: nil)

        } 
}
© www.soinside.com 2019 - 2024. All rights reserved.