2016-06-25 6 views
0

私はUIAlertControllerのテキストフィールドを使用しています。これにより、ユーザーは、データをサーバーに送信する前にファイル名を付けるタイトルを入力できます。しかし、バックエンドはいくつかの理由でファイルを拒否し、エラーを表示する必要があります。ファイル名を入力したのと同じアラートコントローラに、サーバから戻ってくるエラーをどのように表示できますか?サーバー上UIAlertControllerによるエラー処理

class FileController: UIViewController 
{ 

    var alertController: UIAlertController? 

    func savePressed() 
    { 
     createAlert() 
    } 

    func createAlert() 
    { 

     self.alertController = UIAlertController(title: "Save", message: "Name your file.", preferredStyle: .Alert) 

     let saveAsPublicAction = UIAlertAction(title: "Make Public", style: .Default) { (_) in 

     let fileTitle = self.alertController!.textFields![0] as UITextField 

      if fileTitle.text != "" 
      { 
       self.initiateSave(fileTitle.text!, share: true) 
      } 
     } 

     let saveAsPrivateAction = UIAlertAction(title: "Make Private", style: .Default) { (_) in 

      let fileTitle = self.alertController!.textFields![0] as UITextField 

      if fileTitle.text != "" 
      { 
       self.initiateSave(fileTitle.text!, share: false) 
      } 
     } 


     let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in } 

     self.alertController!.addTextFieldWithConfigurationHandler { (textField) in 

      NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification, object: textField, queue: NSOperationQueue.mainQueue()) { (notification) in 
      saveAsPrivateAction.enabled = textField.text != "" 
      saveAsPublicAction.enabled = textField.text != "" 
      } 
     } 

     self.alertController!.addAction(saveAsPublicAction) 
     self.alertController!.addAction(saveAsPrivateAction) 
     self.alertController!.addAction(cancelAction) 

     self.presentViewController(self.alertController!, animated: true, completion: nil) 


    } 
} 

func initiateSave(title:String?, share: Bool?) 
{ 
    //package file 
    initiatePost() 

} 

func initiatePost() 
{ 
    //Send file data to server. Receive any errors and handle 
} 

答えて

1

あなたは、その情報をJSONデータを送信するために多くのロジックを追加することができます。例えば

{ 
    "success": true, 
    "message": "Data received sucessfully" 
} 

要求が成功した場合は、そうでない場合:

{ 
    "success": false, 
    "message": "There is an error" 
} 

だから、あなたはsuccessがfalseの場合はチェックして、内部のエラーメッセージを表示するであろうJSONことを解析messageキーの

+0

私がやっていること。私はファイル名を入力したのと同じアラートコントローラに結果をどのように表示するのだろうかと思っています。 – Brosef

+0

@Brosef okあなたは何を意味するのか分かります。そのためには、再度 'self.presentViewController'を呼び出す必要があります。 – meda

+0

' initiatePost'関数でエラーを受け取り、エラーが受信されたときに警告コントローラはまだ存在しています。アラートコントローラにエラーを表示するにはどうすればよいですか? – Brosef