Reputation: 3095
I have a UIAlertController
with a textfield that allows users to type in a title to name a file before sending the data to the server. However, the backend could reject the file for a couple reasons and an error needs to be displayed. How can I display the error I get back from the server in the same alert controller where I entered the file name?
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
}
Upvotes: 0
Views: 441
Reputation: 45490
On your server you can add more logic to send JSON
data with that information.
For example:
{
"success": true,
"message": "Data received sucessfully"
}
If The request was successful, if not:
{
"success": false,
"message": "There is an error"
}
So when you parse that JSON
you would check if success
is false, and display the error message inside of the message
key.
Upvotes: 1