Ios 警报控制器内多个文本字段的空文本验证
这是我的ui警报控制器。如果任何文本字段为空,我需要禁用“提交”按钮。目前我有一个方法来验证一个文本字段,但我似乎不知道如何将其应用于所有文本字段。这是我的警报控制器和文本字段:Ios 警报控制器内多个文本字段的空文本验证,ios,swift,uitextfield,Ios,Swift,Uitextfield,这是我的ui警报控制器。如果任何文本字段为空,我需要禁用“提交”按钮。目前我有一个方法来验证一个文本字段,但我似乎不知道如何将其应用于所有文本字段。这是我的警报控制器和文本字段: let ac = UIAlertController(title: "Enter Custom Specifications", message: nil, preferredStyle: .alert) ac.addTextField { (textField : UITextField!) ->
let ac = UIAlertController(title: "Enter Custom Specifications", message: nil, preferredStyle: .alert)
ac.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Material Name"
textField.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = ""
textField.keyboardType = UIKeyboardType.decimalPad
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = ""
textField.keyboardType = UIKeyboardType.decimalPad
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = ""
textField.keyboardType = UIKeyboardType.decimalPad
}
使用上面第一个文本字段中调用的函数,我可以对每个文本字段进行正确的空字符串验证,这意味着当第一个文本字段为空时,提交被禁用,但当我开始键入时,提交被启用。如果我将函数调用放入每个textfield声明中,则每个声明都会出现相同的行为
@objc func textFieldDidChange(textField: UITextField){
if textField.text == "" {
submitAction.isEnabled = false
}
else
{
submitAction.isEnabled = true
}
}
我需要一种方法来检查所有文本字段在同一时间和提交禁用,如果其中任何一个是空的
编辑:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField.text == "" {
//You can perfrom your activity here when textfield got blank or empty
submitAction.isEnabled = false
}
else if textField.text != ""
{
submitAction.isEnabled = true
//You can perfrom your activity here when textfield got not blank or not empty
}
//add your code here...
return true
}
编辑2:
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
if let text = (textField.text as? NSString)?.replacingCharacters(in: range, with: string), !text.isEmpty {
if let text = textField.text?.replacingCharacters(in: range, with: string), !text.isEmpty {
submitAction.isEnabled = true
} else {
submitAction.isEnabled = false
}
return true
}}
编辑3:
我可以在创建文本字段时将其命名为textfield“x”,而不是将所有文本字段都称为“textfield”:
ac.addTextField { (textField2 : UITextField!) -> Void in
textField2.placeholder = "Column Width (in)"
textField2.keyboardType = UIKeyboardType.decimalPad
textField2.delegate = self
}
我怎样才能让shouldChangeCharacters同时接受它们呢
编辑(解决方案):
好的,我算出来了。PGDev在逻辑部分是正确的,但不是用于实际检查的委托函数。我要将他的答案标记为正确的,因为没有它,我不可能达到这一点
最初我们使用的是TextFieldDiEndediting,我认为它根本不起作用,因为一旦四个文本字段全部填充,它就无法重新启用submit按钮。我没有意识到的是,在调用TextFieldDiEndediting之后,我必须点击一个额外的textfield。这很有意义,因为它有“EndEditing”以我的名义
我使用了PGDevs逻辑并将其放入shouldChangeCharacters中,该语句工作正常但并不完美。我不确定它是否存在内部问题,但当您尝试填充最后一个文本框时,有时会将backspace(或其他内容)解释为一个字符,它会异常地抛出enable/!enable语句
textFieldDidChange使用PGDevs逻辑就是答案
设置要添加到警报控制器的文本框数量的类范围变量:
var textField1: UITextField?
var textField2: UITextField?
var textField3: UITextField?
var textField4: UITextField?
将此函数放在类函数的底部:
@objc func textFieldDidChange(_ textField: UITextField) {
let text1 = self.textField1?.text ?? ""
let text2 = self.textField2?.text ?? ""
let text3 = self.textField3?.text ?? ""
let text4 = self.textField4?.text ?? ""
if !text1.isEmpty && !text2.isEmpty && !text3.isEmpty && !text4.isEmpty {
submitAction.isEnabled = true
} else {
submitAction.isEnabled = false
}
}
let ac = UIAlertController(title: "Enter Custom Specifications", message: nil, preferredStyle: .alert)
ac.popoverPresentationController?.sourceView = self.view
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.placeholder = "Material Name"
self.textField1 = textField
textField.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)
}
在警报控制器中生成实际文本字段时,调用上述函数:
@objc func textFieldDidChange(_ textField: UITextField) {
let text1 = self.textField1?.text ?? ""
let text2 = self.textField2?.text ?? ""
let text3 = self.textField3?.text ?? ""
let text4 = self.textField4?.text ?? ""
if !text1.isEmpty && !text2.isEmpty && !text3.isEmpty && !text4.isEmpty {
submitAction.isEnabled = true
} else {
submitAction.isEnabled = false
}
}
let ac = UIAlertController(title: "Enter Custom Specifications", message: nil, preferredStyle: .alert)
ac.popoverPresentationController?.sourceView = self.view
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.placeholder = "Material Name"
self.textField1 = textField
textField.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)
}
这非常适合我。除非所有4行中至少有一个字符,否则提交将保持禁用状态。这可以用于任意多个文本字段。尝试将每个文本字段的代理设置为显示警报的viewCobtroller,即
textField.delegate = self
使ViewController符合UITextFieldDelegate协议
class ViewController: UIViewController, UITextFieldDelegate {
实现相关的UITextFieldDelegate
方法,例如:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = (textField.text as? NSString)?.replacingCharacters(in: range, with: string), !text.isEmpty {
submitAction.isEnabled = true
} else {
submitAction.isEnabled = false
}
return true
}
编辑:
您需要保留对所有文本字段的引用
,以便您可以检查所有字段中的文本
,以启用/禁用提交
,即
var textField1: UITextField?
var textField2: UITextField?
var textField3: UITextField?
var textField4: UITextField?
let ac = UIAlertController(title: "Enter Custom Specifications", message: nil, preferredStyle: .alert)
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.placeholder = "Material Name"
self.textField1 = textField
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.keyboardType = UIKeyboardType.decimalPad
self.textField2 = textField
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.keyboardType = UIKeyboardType.decimalPad
self.textField3 = textField
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.keyboardType = UIKeyboardType.decimalPad
self.textField4 = textField
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.validate()
}
func validate() {
let text1 = self.textField1?.text ?? ""
let text2 = self.textField2?.text ?? ""
let text3 = self.textField3?.text ?? ""
let text4 = self.textField4?.text ?? ""
if !text1.isEmpty && !text2.isEmpty && !text3.isEmpty && !text4.isEmpty {
submitAction.isEnabled = true
} else {
submitAction.isEnabled = false
}
}
在添加到UIAlertController
时,设置每个文本字段,即
var textField1: UITextField?
var textField2: UITextField?
var textField3: UITextField?
var textField4: UITextField?
let ac = UIAlertController(title: "Enter Custom Specifications", message: nil, preferredStyle: .alert)
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.placeholder = "Material Name"
self.textField1 = textField
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.keyboardType = UIKeyboardType.decimalPad
self.textField2 = textField
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.keyboardType = UIKeyboardType.decimalPad
self.textField3 = textField
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.keyboardType = UIKeyboardType.decimalPad
self.textField4 = textField
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.validate()
}
func validate() {
let text1 = self.textField1?.text ?? ""
let text2 = self.textField2?.text ?? ""
let text3 = self.textField3?.text ?? ""
let text4 = self.textField4?.text ?? ""
if !text1.isEmpty && !text2.isEmpty && !text3.isEmpty && !text4.isEmpty {
submitAction.isEnabled = true
} else {
submitAction.isEnabled = false
}
}
每次在任何文本字段中发生更改时调用validate()
,即
var textField1: UITextField?
var textField2: UITextField?
var textField3: UITextField?
var textField4: UITextField?
let ac = UIAlertController(title: "Enter Custom Specifications", message: nil, preferredStyle: .alert)
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.placeholder = "Material Name"
self.textField1 = textField
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.keyboardType = UIKeyboardType.decimalPad
self.textField2 = textField
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.keyboardType = UIKeyboardType.decimalPad
self.textField3 = textField
}
ac.addTextField { (textField : UITextField!) -> Void in
textField.delegate = self
textField.keyboardType = UIKeyboardType.decimalPad
self.textField4 = textField
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.validate()
}
func validate() {
let text1 = self.textField1?.text ?? ""
let text2 = self.textField2?.text ?? ""
let text3 = self.textField3?.text ?? ""
let text4 = self.textField4?.text ?? ""
if !text1.isEmpty && !text2.isEmpty && !text3.isEmpty && !text4.isEmpty {
submitAction.isEnabled = true
} else {
submitAction.isEnabled = false
}
}
编辑了答案。您需要检查在textField
textField中创建的最终字符串。shouldChangeCharactersIn
方法中的text
在编辑前给出字符串。您使用的是哪个Swift版本?更新了代码。只需将其键入NSString
。为方法shouldchangeCharactere添加准确的代码rsIn
这样我就可以看一看了。只要将准确的代码和方法签名复制粘贴到您的文件中,看看您是否仍然面临这个问题,