我已经设置了4个TextField并通过textFieldDidChange
连接了它们。现在假设我输入了错误的验证,我希望能够再次选择TextField
并清除所有最后的输入。我在Swift 3中尝试了didBeginEditing
,但它不起作用,因为它只清除了一个TextField
而不是全部四个TextField
。请帮忙。
import UIKit
class verificationVC: UIViewController,UITextFieldDelegate, UITextViewDelegate {
@IBOutlet weak var codefirstText: UITextField!
@IBOutlet weak var codesecondText: UITextField!
@IBOutlet weak var codethirdText: UITextField!
@IBOutlet weak var codefourthText: UITextField!
func textFieldDidBeginEditing(textField: UITextField)
{
codefirstText.text = ""
codesecondText.text = ""
codethirdText.text = ""
codefourthText.text = ""
if (codefirstText.text != nil)
{
codefirstText.text = ""
codesecondText.text = ""
codethirdText.text = ""
codefourthText.text = ""
}
else if (codesecondText.text != nil)
{
codefirstText.text = ""
codesecondText.text = ""
codethirdText.text = ""
codefourthText.text = ""
}
else if (codethirdText.text != nil)
{
codefirstText.text = ""
codesecondText.text = ""
codethirdText.text = ""
codefourthText.text = ""
}
else if (codefourthText.text != nil)
{
codefirstText.text = ""
codesecondText.text = ""
codethirdText.text = ""
codefourthText.text = ""
}
}
func textFieldDidChange (textfield: UITextField)
{
let text = textfield.text
if text?.utf16.count == 1
{
switch textfield {
case codefirstText:
codesecondText.becomeFirstResponder()
case codesecondText:
codethirdText.becomeFirstResponder()
case codethirdText:
codefourthText.becomeFirstResponder()
case codefourthText:
codefourthText.resignFirstResponder()
default:
break
}
}
答案 0 :(得分:0)
由于在Swift中输入的类型,字符串不能是nil
。您是否尝试使用空字符串而不是nil
值来控制文本字段值:
func textFieldDidBeginEditing(textField: UITextField)
{
codefirstText.text = ""
codesecondText.text = ""
codethirdText.text = ""
codefourthText.text = ""
if (codefirstText.text != "")
{
codefirstText.text = ""
codesecondText.text = ""
codethirdText.text = ""
codefourthText.text = ""
}
else if (codesecondText.text != "")
{
codefirstText.text = ""
codesecondText.text = ""
codethirdText.text = ""
codefourthText.text = ""
}
else if (codethirdText.text != "")
{
codefirstText.text = ""
codesecondText.text = ""
codethirdText.text = ""
codefourthText.text = ""
}
else if (codefourthText.text != "")
{
codefirstText.text = ""
codesecondText.text = ""
codethirdText.text = ""
codefourthText.text = ""
}
}