按下按钮后 UIAlert 验证中的 Swift3 UITextField

Swift3 UITextField in UIAlert validation after button pressed

我已经实现了带有两个文本字段的警报 window。我想执行一些验证:如果其中一个文本字段为空(用户未输入任何值),应用程序不应允许用户按下 "Done" 按钮。 我不想显示任何警报,只想不允许用户保存空白数据。

我创建了下面列出的函数。添加了两个 return 的守卫。但是在这种情况下,如果用户没有输入任何内容,警报就会关闭并且不会保存任何内容。我不想关闭警报 window。

能做到怎么办?还没找到答案。我检查了 个问题,但看起来它不适用于我。感谢您的帮助!

private func addTable() {
    let alert = UIAlertController(title: NSLocalizedString("inputTableParams", comment: ""), message: nil, preferredStyle: .alert)
    alert.addTextField(configurationHandler: configureTableNameTextField)
    alert.addTextField(configurationHandler: configureTableCapacityTextField)
    alert.textFields?[0].autocapitalizationType = .sentences
    alert.textFields?[1].autocapitalizationType = .sentences
    alert.addAction(UIAlertAction(title: NSLocalizedString("alertCancel", comment: ""), style: .cancel, handler: nil))
    alert.addAction(UIAlertAction(title: NSLocalizedString("alertDone", comment: ""), style: .default, handler: { (UIAlertAction) in
        guard self.tableNameTextField.text != "" else {return}
        guard self.tableCapacityTextField.text != "" else {return}
        let newTable = Table(tableName: self.tableNameTextField.text!, tableCapacity: Int(self.tableCapacityTextField.text!)!)
        let result = try? TablesTable.getOrCreateTable(table: newTable)
        if result != nil {
            self.updateTableView()
        }
    }))
    self.present(alert, animated: true, completion: {})
}

看起来不可能完全按照我的要求做,所以我实施了另一个错误警报 window。

//MARK: Functions
//Functions for Alert window for adding table
private func configureTableNameTextField (textField: UITextField!) {
    textField.placeholder = NSLocalizedString("tableName", comment: "")
    textField.keyboardType = .default
    tableNameTextField = textField
}
private func configureTableCapacityTextField (textField: UITextField!) {
    textField.placeholder = NSLocalizedString("tableCapacity", comment: "")
    textField.keyboardType = .numberPad
    tableCapacityTextField = textField
}

private func showAlertParamsNotFilledProperly() {
    let alertNoCanDo = UIAlertController(title: NSLocalizedString("alertNoCanDo", comment: ""), message: NSLocalizedString("paramsNotFilledProperly", comment: ""), preferredStyle: .alert)
    alertNoCanDo.addAction(UIAlertAction(title: NSLocalizedString("alertDone", comment: ""), style: .cancel, handler: nil))
    self.present(alertNoCanDo, animated: true, completion: {})
}

private func showAlertUnableToSave() {
    let alertNoCanDo = UIAlertController(title: NSLocalizedString("alertUnableToSaveData", comment: ""), message: NSLocalizedString("checkInputParameters", comment: ""), preferredStyle: .alert)
    alertNoCanDo.addAction(UIAlertAction(title: NSLocalizedString("alertDone", comment: ""), style: .cancel, handler: nil))
    self.present(alertNoCanDo, animated: true, completion: {})
}

//Functions for managing tables
private func addTable() {
    let alert = UIAlertController(title: NSLocalizedString("inputTableParams", comment: ""), message: nil, preferredStyle: .alert)
    alert.addTextField(configurationHandler: configureTableNameTextField)
    alert.addTextField(configurationHandler: configureTableCapacityTextField)
    alert.textFields?[0].autocapitalizationType = .sentences
    alert.textFields?[1].autocapitalizationType = .sentences
    alert.addAction(UIAlertAction(title: NSLocalizedString("alertCancel", comment: ""), style: .cancel, handler: nil))
    alert.addAction(UIAlertAction(title: NSLocalizedString("alertDone", comment: ""), style: .default, handler: { (UIAlertAction) in
        if self.tableNameTextField.text == "" || self.tableCapacityTextField.text == "" {
            self.showAlertParamsNotFilledProperly()
            return
        }
        if let number = NumberFormatter().number(from: self.tableCapacityTextField.text!) {
            let capacity = Int(number)
            let newTable = Table(tableName: self.tableNameTextField.text!, tableCapacity: capacity)
            let result = try? TablesTable.getOrCreateTable(table: newTable)
            if result != nil {
                self.updateTableView()
            }
        } else {
            self.showAlertParamsNotFilledProperly()
            return
        }
    }))
    self.present(alert, animated: true, completion: {})
}