2017-02-01 42 views
0

UIAlertControllers未被執行或未被顯示。如果我只寫一個打印語句,那麼在控制檯中有輸出,但是現在打印語句也沒有被執行(如果我將它們和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

你在哪裏呈現警報控制器?我沒有看到任何要出席的電話。 – Samantha

+0

許多'if-else'語句看起來不太好 - 你應該改進它。例如,您可以使用開關盒。例如響應[處理鏈接](https://github.com/Alamofire/Alamofire#chained-response-handlers)。您可以發佈錯誤JSON – muescha

+0

的示例(一個或兩個),您可以對此使用響應驗證。 doc和示例[這裏](https://github.com/Alamofire/Alamofire#response-validation)和[here](https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0% 20Migration%20Guide.md#response-validation) – muescha

回答

0

需要出示alertcontroller後addAction

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

也許你必須提交在一個新的操作

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

有時候我真的認爲多線程是邪惡的,而且通過簡單的關閉很快就會變得更糟。我猜這是本週的第二十個問題,與你有類似的解決方案,例如,有人想從非主線程中在UI中做些什麼:-) –

+0

感謝@Retterdesdialogs的代碼和建議,它解決了我的問題。 –

+0

@AndreasOetjen你是對的。我剛剛提到過,因爲也許他想在請求後更改爲另一個控制器。另外我沒有看到任何委託協議和實現不看起來專業,所以也許他會卡住,因爲主線程被阻塞的原因。 – Retterdesdialogs

相關問題