Swift3 UITextField in UIAlert validation after button pressed

↘锁芯ラ 提交于 2020-01-07 04:39:05

问题


I have implemented alert window with two text fields. And I want to perform some validation: if one of text fields is blank (user did not input any value), application should not allow user to press the "Done" button. I don't want to show any alerts, just want to not allow user to save blank data.

I have created the function listed below. Added two guards with return. But in this case if user did not input anything, alert just closes and nothing is saved. I don't want alert window to be closed.

How can it be done if it can be? Have not found the answer. I have checked this question, but looks like it's not applicable for me. Appreciate your help!

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: {})
}

回答1:


It looks like it is not possible to do exactly what I want, so I have implemented another alert window with error.

//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: {})
}


来源:https://stackoverflow.com/questions/45310261/swift3-uitextfield-in-uialert-validation-after-button-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!