Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift中验证文本输入_Ios_Swift_Xcode_Validation - Fatal编程技术网

Ios 在swift中验证文本输入

Ios 在swift中验证文本输入,ios,swift,xcode,validation,Ios,Swift,Xcode,Validation,我的密码坏了,你能指出什么地方出了问题吗。我正在尝试验证SNameInput中的用户输入,当输入无效时,错误标签应更改其文本,当用户在SNameInput中键入且其有效时,STitle应更改其文本,谢谢 func textFieldDidChange(SNameInput: UITextField) { let d = "" if (SNameInput.isEqual(d)||(SNameInput.text?.characters.count)! >=

我的密码坏了,你能指出什么地方出了问题吗。我正在尝试验证SNameInput中的用户输入,当输入无效时,错误标签应更改其文本,当用户在SNameInput中键入且其有效时,STitle应更改其文本,谢谢

func textFieldDidChange(SNameInput: UITextField) {
        let d = ""
        if (SNameInput.isEqual(d)||(SNameInput.text?.characters.count)! >= 21) {
            errorLabel.text = "Name has to be a t least 1 character and not longer than 20"}
        else{  errorLabel.text = ""
            Stitle.text = SNameInput.text}
    }

可能性:您没有将
editingDidEnd
操作添加到
SNameInput
文本字段中

editingDidEnd
:在
UITextField
对象离开其边界


您是否将textfield delegate设置为
self
?您的委托方法是否被调用?您有一些奇怪的表达式,如(SNameInput:UITextField)SNameInput.isEqual(d)。另外,你的英语句子很难理解,因为你没有用句号结束每个句子。
override func viewDidLoad() {
    super.viewDidLoad()

    SNameInput.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingDidEnd)
}

@IBAction func textFieldDidChange(_ sender: UITextField) {
    guard let string = sender.text else { return }

    if (string.isEmpty || string.characters.count >= 21) {
        errorLabel.text = "Name has to be a t least 1 character and not longer than 20"
    }
    else{
        errorLabel.text = ""
        Stitle.text = string
    }
}