Ios Swift 5-通过UIAlertView在文本字段中进行验证

Ios Swift 5-通过UIAlertView在文本字段中进行验证,ios,swift,core-data,uialertview,uialertcontroller,Ios,Swift,Core Data,Uialertview,Uialertcontroller,因此,我正在使用swift5并处理核心数据。我正试图通过UIAlertView向文本字段添加验证 要澄清我的代码已经做了什么,请执行以下操作: 用户按下一个按钮,iOS照片库界面弹出 选择照片后,会弹出一个UIAlertView,要求我在两个文本字段中输入两个文本 完成后,数据将提交到数据库,并显示我输入到TableViewCell 问题? 提交空白文本字段时,不会提示用户输入文本。它只会在UITableViewCell中显示图像,而不显示任何文本,这是在不输入字符串时的明显结果 我想要实现

因此,我正在使用
swift5
并处理
核心数据。我正试图通过
UIAlertView
向文本字段添加验证

要澄清我的代码已经做了什么,请执行以下操作:

  • 用户按下一个按钮,iOS照片库界面弹出
  • 选择照片后,会弹出一个
    UIAlertView
    ,要求我在两个文本字段中输入两个文本
  • 完成后,数据将提交到数据库,并显示我输入到
    TableViewCell
问题?

提交空白文本字段时,不会提示用户输入文本。它只会在
UITableViewCell
中显示图像,而不显示任何文本,这是在不输入字符串时的明显结果

我想要实现什么?

在用户未输入文本时向其添加验证消息,并取消向数据库提交的过程

if (itemTextField?.text!.isEmpty)! || (productTextField?.text!.isEmpty)! {

            let alertBlankInput = UIAlertController(title: "Blank Input", message: "Please don't leave the textfields empty.", preferredStyle: .alert)

            let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel)

            alertBlankInput.addAction(okAction)

            self.present(alertBlankInput, animated: true, completion: nil)
        }
let brandItem = Brand(context: managedObjectContext)
        brandItem.image = NSData(data: image.jpegData(compressionQuality: 0.3)!) as Data
我已经试过了什么?

请参阅下面的代码。注意:向数据库提交图像和文本已经有效,我的问题是文本字段验证

分解代码

下面的代码片段是我尝试添加到
createBrandItem
函数的内容。当我在调试应用程序时添加此
if
语句时,它会使用此条件语句,但也会在添加到数据库的位置完成条件语句

if (itemTextField?.text!.isEmpty)! || (productTextField?.text!.isEmpty)! {

            let alertBlankInput = UIAlertController(title: "Blank Input", message: "Please don't leave the textfields empty.", preferredStyle: .alert)

            let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel)

            alertBlankInput.addAction(okAction)

            self.present(alertBlankInput, animated: true, completion: nil)
        }
let brandItem = Brand(context: managedObjectContext)
        brandItem.image = NSData(data: image.jpegData(compressionQuality: 0.3)!) as Data
下面的代码是将数据添加到数据库的代码段

if (itemTextField?.text!.isEmpty)! || (productTextField?.text!.isEmpty)! {

            let alertBlankInput = UIAlertController(title: "Blank Input", message: "Please don't leave the textfields empty.", preferredStyle: .alert)

            let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel)

            alertBlankInput.addAction(okAction)

            self.present(alertBlankInput, animated: true, completion: nil)
        }
let brandItem = Brand(context: managedObjectContext)
        brandItem.image = NSData(data: image.jpegData(compressionQuality: 0.3)!) as Data

摘要


如果文本字段为空并停止提交到数据库的过程,我该怎么办?

我建议您创建自己的子视图控制器,并将其显示为模型对话框,这样会更优雅,让您能够更好地控制流程。但是,如果要继续使用警报控制器,可以通过在禁用状态下创建OK操作使其工作:

let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel)
okActions.isEnabled = false
inputAlert.addAction(okAction)
然后使用textFields的委托验证文本字段的内容(您需要有一种方法来协调这两个文本字段-您可以使用alertController的
textFields?
数组进行交叉检查,或者使用局部变量作为标志),并且当您对这两个文本字段的内容满意时

inputAlert.actions.filter({$0.title == "OK"  }).first?.isEnabled = true