首页 > 解决方案 > 如何修复警报

问题描述

我写了一个函数来输出警报,但它不起作用。

这是功能:

if titleTextField.text.isEmpty == false {
            if let result = DataManager.shared.quizController.joinQuiz(id: titleTextField.text) {
                switch result {
                case .badCode :
                    self.showErrorAlert(message: "Bad code")
                case .joined:
                    self.showErrorAlert(message: "You are alredy joined")
                case .notJoined:
                    navigationController?.popToRootViewController(animated: true)

我希望这会输出一些警报,但是我根本没有得到任何输出。

标签: swiftxcode

解决方案


这是显示警报的简单方法

if titleTextField.text.isEmpty == false {
            if let result = DataManager.shared.quizController.joinQuiz(id: titleTextField.text) {
                switch result {
                case .badCode :
                    self.showErrorAlert(message: "Bad code")
                case .joined:
                    self.showErrorAlert(message: "You are alredy joined")
                case .notJoined:
                    navigationController?.popToRootViewController(animated: true)
                }
       }
//function for showing alert message

    func showErrorAlert(_ with message: String){
        let alert = UIAlertController(title: "", message: message, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "ok", style: .default, handler: nil)
        alert.addAction(okAction)
        self.present(alert, animated: true, completion: nil)
    }

推荐阅读