2017-02-01 1 views
0

UIAlertControllersは実行されていないか、表示されていません。私が単独でprint文を書くと、コンソールに出力がありますが、print文も実行されていません(私が以下に書いたコードのようにUIAlertControllersと一緒にそれらを書くと)。Alamofire reponseJSONブロック内でUIAlertControllerを使用したい

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

をそれを提示する必要がaddAction

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

後alertcontrollerを提示する必要がありますか?私は提示する必要はありません。 – Samantha

+0

多くの 'if-else'文はあまりうまく見えません。たとえば、スイッチケースを使用できます。例えばレスポンス[連鎖を扱う](https://github.com/Alamofire/Alamofire#chained-response-handlers)を使用します。 JSON – muescha

+0

のエラーの例(1つまたは2つ)を投稿することができます。これに対して応答の検証を使用することができます。 docとexamples [ここ](https://github.com/Alamofire/Alamofire#response-validation)と[ここ](https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%) 20Migration%20Guide.md#response-validation) – muescha

答えて

0

あなたは多分あなたは新しい操作でアラートコントローラを提示している

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

時には、私は本当にマルチスレッドが悪いと思っています。簡単に閉じてしまうと速くなり、さらに悪化します。今週の20番目の質問で、あなたと同じような解決策があると思います。誰かがメインではないスレッドから何かをしたいと思っています:-) –

+0

コードと提案のために@Retterdesdialogsに感謝し、私の問題を解決しました。 –

+0

@AndreasOetjenあなたは絶対に正しいです。私はちょうどそれを言いました、多分彼は要求の後に別のコントローラに変更したいと思うので。また、私はデリゲートプロトコルを見ていないし、実装はその専門家を見ていないので、主なスレッドが理由でブロックされているので、おそらく彼は立ち往生します。 – Retterdesdialogs

関連する問題