我已经实现了包含两个文本字段的警报窗口。我想执行一些验证:如果其中一个文本字段为空(用户未输入任何值),则应用程序不应允许用户按“完成”按钮。 我不想显示任何警报,只是想不允许用户保存空白数据。
我创建了下面列出的功能。增加了两名带回归的守卫。但是在这种情况下,如果用户没有输入任何内容,则仅关闭警报并且不保存任何内容。我不希望关闭警报窗口。
如果可以,怎么办呢?还没有找到答案。我已经检查了this个问题,但看起来它并不适用于我。感谢您的帮助!
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: {})
}
答案 0 :(得分:0)
看起来不可能完全按照我的意愿行事,所以我实施了另一个错误的警报窗口。
//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: {})
}