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

Posted

技术标签:

【中文标题】按下按钮后 UIAlert 验证中的 Swift3 UITextField【英文标题】:Swift3 UITextField in UIAlert validation after button pressed 【发布时间】:2017-07-25 17:41:38 【问题描述】:

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

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

如果可以怎么办?还没有找到答案。我已经检查了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: )

【问题讨论】:

【参考方案1】:

看起来不可能完全按照我的意愿去做,所以我实现了另一个出现错误的警报窗口。

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

【讨论】:

以上是关于按下按钮后 UIAlert 验证中的 Swift3 UITextField的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3 游戏按钮按下循环

按下后退按钮时 UITableView 中的数据会重复

Swift 3.0 中的 UIAlertAction 内的 UIAlertView ?

按下按钮后在 React 中重新填充/刷新表

swift 3 在tableView 中按下了哪个单元格按钮

UIAlert 和视图控制器的错误