ios - 想要在 Alamofire reponseJSON block 中使用 UIAlertController

标签 ios swift alamofire uialertcontroller

UIAlertController 未执行或未显示。如果我只编写一条打印语句,那么控制台中会有输出,但现在打印语句也不会被执行(如果我将它们与 UIAlertControllers 一起编写,就像我在下面编写的代码中一样)。

 Alamofire.request(some_url, method: .post, parameters: data, encoding: URLEncoding.default, headers: nil).responseJSON{
                response in

                let json = JSON(response.result.value)

                print(json)
                self.eventid = json[0]["EventRegID"].stringValue

                if !json[0]["AuthKeyError"].exists(){
                    if !json[0]["ExceptionOccured"].exists(){
                        if !json[0]["RegistrationFailed"].exists() {
                            if !json[0]["EventInHold"].exists() {
                                if json[0]["RegistrationSuccess"].exists() {
                                    let alertController = UIAlertController(title: "", message: json[0]["RegistrationSuccess"].stringValue, preferredStyle: .alert)
                                    let no1Action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
                                        print("The user has registered successfully")
                                    }
                                    alertController.addAction(no1Action)
                                }
                                else{

                                }
                            }
                            else {
                                let alertController = UIAlertController(title: "", message: "Event is on hold.", preferredStyle: .alert)
                                let no2Action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
                                    print("The event is on hold.")
                                }
                                let yes2Action = UIAlertAction(title: "GO", style: .default) { (action) -> Void in
                                    self.performSegue(withIdentifier: "bullshit", sender: self)
                                }
                                alertController.addAction(no2Action)
                                alertController.addAction(yes2Action)
                            }
                        }
                        else {
                            print("Registration failed due to connection issues. Please login.")
                            let alertController = UIAlertController(title: "", message: "Registration failed", preferredStyle: .alert)
                            let no3Action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
                                print("The registration failed")
                            }
                            alertController.addAction(no3Action)
                        }
                    }
                    else {
                        print("There's some problem with the database")
                        let alertController = UIAlertController(title: "", message: "Some problem with the server", preferredStyle: .alert)
                        let no4Action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
                            print("The user has registered successfully")
                        }
                        alertController.addAction(no4Action)
                    }
                }
                else {
                    print("AuthKeyError")
                    let alertController = UIAlertController(title: "", message: "Auth key error", preferredStyle: .alert)
                    let no5Action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
                        print("AAUTH KEY ERROR")
                    }
                    alertController.addAction(no5Action)
                }
            }

        }
        else {
            print("not ok")

        }



    }

最佳答案

您需要在addAction之后呈现alertcontroller

presentViewController(alertController, animated: true, completion: nil)

也许您必须在新操作中呈现它

OperationQueue.main.addOperation {
   presentViewController(alertController, animated: true, completion: nil)
}

关于ios - 想要在 Alamofire reponseJSON block 中使用 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41985589/

相关文章:

objective-c - UIActionSheet 在横向模式下不旋转?

Swift 扩展协议(protocol)

ios - Truetime iOS Framework 出现超时错误

ios - 通过 AlamofireImage 下载 UIImage?

ios - Alamofire finishHandler 更新 ui 仅在函数完成时才起作用

iOS显示标题和段落

iphone - 创建点击叠加说明 View

ios - 以编程方式将 UILabel 添加到自定义 tableViewCell

ios - Swift 中有复选框吗?

swift - 请求 REST POST API 在 Postman 中工作,但在 Alamofire 中不起作用