UIAlertController не отображается - PullRequest
0 голосов
/ 08 мая 2020

это мой 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, но пользовательский интерфейс предупреждений не отображается в симуляторе . Вот что происходит, когда я нажимаю кнопку запроса (вывод отображается только в Xcode):

enter image description here

1 Ответ

0 голосов
/ 08 мая 2020

Вы не представляете контроллер в основном потоке, поэтому попробуйте следующее:

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)

        } 
}
...