ios - 使用 UIAlertController 进行错误处理

标签 ios swift uialertcontroller

我有一个带有文本字段的UIAlertController,允许用户在将数据发送到服务器之前输入标题来命名文件。但是,后端可能会因多种原因拒绝该文件,并且需要显示错误。如何在我输入文件名的同一警报 Controller 中显示从服务器返回的错误?

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
}

最佳答案

在您的服务器上,您可以添加更多逻辑来发送包含该信息的 JSON 数据。

例如:

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

如果请求成功,如果不成功:

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

因此,当您解析该 JSON 时,您将检查 success 是否为 false,并在 message 键内显示错误消息。

关于ios - 使用 UIAlertController 进行错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38024249/

相关文章:

ios - Objective-C 编写通用的 getter 和 setter 方法

ios - [self.navigationController pushViewController :ngView animated:YES]; not working

ios - UIAlertController 因在 backgroundThread Objective-C 中出现而崩溃

ios - UIAlertController 在关闭后一直重新出现

ios - CocoaPods 安装 SlackTextViewController 的 Swift/Xcode 7 问题

ios - 如何在 xcode 的单 View 应用程序中创建带有按钮的方向键

ios - 如何将通用(关联类型需要)协议(protocol)作为委托(delegate)给任何 Controller ?

ios - swift 2 : Type of expression is ambiguous without more context

带有 3 个原型(prototype)单元和自动高度的 Swift tableView

ios - Swift 将箭头和 sourceView 添加到 UIAlertController