这是我的ui警报控制器。如果任何文本字段为空,则需要禁用提交按钮。目前,我有一种方法可以验证单个文本字段,但是我似乎无法弄清楚如何将其应用于所有文本字段。这是我的警报控制器和文本字段:
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
}
使用在上面的第一个文本字段中调用的此函数,我可以对每个文本字段进行正确的空字符串验证,这意味着当第一个文本字段为空时,提交被禁用,但是当我开始键入时,提交被启用。如果将函数调用放在每个文本字段声明中,则每个文本字段都存在相同的行为。
@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
}
EDIT2:
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
}
我如何获取shouldChangeCharactersIn以便同时接受它们?
编辑(解决方案):
好吧,我知道了。 PGDev在其逻辑部分上是正确的,但不是用于实际检查的委托函数。我要把他的答案标记为正确,因为如果没有它,我将无法理解这一点。
最初,我们使用的是textFieldDidEndEditing,我认为它只是行不通,因为一旦填充了所有四个文本字段,它就不会重新启用提交按钮。我没有意识到的是,在完成调用textFieldDidEndEditing的操作后,我不得不点击一个附加的文本字段。有道理,因为它的名称中带有“ EndEditing”。
我采用了PGDevs逻辑,并将其放入shouldChangeCharactersIn中,该方法工作正常但并不完美。我不确定它是否存在内部问题,但是当您尝试填充最后一个文本框时,有时会将退格键(或其他内容)解释为字符,并且会把enable /!enable语句丢掉。
答案是使用PGDevs逻辑的textFieldDidChange。
设置要添加到警报控制器中的文本框的类范围变量:
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)
}
这对我来说非常有效。除非所有4行中至少有一个字符,否则提交保持禁用状态。可以根据需要将其用于任意多个文本字段。
答案 0 :(得分:2)
尝试将每个textField的委托设置为要在其中显示警报的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
}
编辑:
您需要将reference
保留在所有textFields
上,以便可以将所有text
中的enable/disable
检查到submitAction
var textField1: UITextField?
var textField2: UITextField?
var textField3: UITextField?
var textField4: UITextField?
,即< / p>
textFields
设置每个UIAlertController
,同时添加到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
}
,即
validate()
每次textFields
发生任何变化(即
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
}
}
----