想在Alamofire reponseJSON块中使用UIAlertController

时间:2017-02-01 17:09:29

标签: ios swift alamofire uialertcontroller

UIAlertControllers未执行或未显示。如果我只写一个print语句然后在控制台输出,但现在print语句也没有被执行(如果我把它们与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")

        }



    }

1 个答案:

答案 0 :(得分:0)

您需要在addAction

之后提供alertcontroller
presentViewController(alertController, animated: true, completion: nil)

也许你必须在新的操作中提出它

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