textFieldShouldEndEditing 故障?

textFieldShouldEndEditing malfunction?

我实现了这个方法,当在文本字段中输入了未经授权的字符或已使用的用户名时,会向用户发送多个警报视图:

func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    let acceptedChars = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_")

    var isTaken: Bool = false

    for c in usernameTxt.text.utf16 {
        if !acceptedChars.characterIsMember(c) {

            let myAlert = SCLAlertView().showError("Woah There", subTitle: "username can only contain uppercase & lowercase letters A-Z,numbers 0-9, or the special character(s) _", closeButtonTitle: "Got It")
            myAlert.alertview.contentView.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
            myAlert.alertview.circleBG.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
            myAlert.alertview.labelTitle.textColor = UIColor.whiteColor()
            myAlert.alertview.contentView.layer.borderColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0).CGColor
            myAlert.alertview.viewText.textColor = UIColor.whiteColor()
            myAlert.alertview.viewText.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)

            signSecond.userInteractionEnabled = false
            signSecond.highlighted = true

            textField.resignFirstResponder()

            return true

        }else {
            signSecond.userInteractionEnabled = true
            signSecond.highlighted = false
        }
    }; for b in passwordTxt.text.utf16 {
        if !acceptedChars.characterIsMember(b) {

            let myAlert = SCLAlertView().showError("Woah There", subTitle: "password can only contain uppercase & lowercase letters A-Z,numbers 0-9, or the special character(s) _", closeButtonTitle: "Got It")
            myAlert.alertview.contentView.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
            myAlert.alertview.circleBG.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
            myAlert.alertview.labelTitle.textColor = UIColor.whiteColor()
            myAlert.alertview.contentView.layer.borderColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0).CGColor
            myAlert.alertview.viewText.textColor = UIColor.whiteColor()
            myAlert.alertview.viewText.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)

            signSecond.userInteractionEnabled = false
            signSecond.highlighted = true

            textField.resignFirstResponder()

            return true
        }else {
            signSecond.userInteractionEnabled = true
            signSecond.highlighted = false
        }
    }; for a in confirmTxt.text.utf16 {
        if !acceptedChars.characterIsMember(a) {

            let myAlert = SCLAlertView().showError("Woah There", subTitle: "password confirmation can only contain uppercase & lowercase letters A-Z,numbers 0-9, or the special character(s) _", closeButtonTitle: "Got It")
            myAlert.alertview.contentView.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
            myAlert.alertview.circleBG.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
            myAlert.alertview.labelTitle.textColor = UIColor.whiteColor()
            myAlert.alertview.contentView.layer.borderColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0).CGColor
            myAlert.alertview.viewText.textColor = UIColor.whiteColor()
            myAlert.alertview.viewText.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)

            signSecond.userInteractionEnabled = false
            signSecond.highlighted = true
            textField.resignFirstResponder()

            return true
        }else {
            signSecond.userInteractionEnabled = true
            signSecond.highlighted = false
        }
    };

    var query = PFQuery(className: "_User")
    query.whereKey("username", equalTo: usernameTxt.text)
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) in
        if error == nil {
            if (objects!.count > 0){
                isTaken = true
                let myAlert = SCLAlertView().showError("Woah There", subTitle: "username \(self.usernameTxt.text) is already taken", closeButtonTitle: "Got It")
                myAlert.alertview.contentView.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
                myAlert.alertview.circleBG.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
                myAlert.alertview.labelTitle.textColor = UIColor.whiteColor()
                myAlert.alertview.contentView.layer.borderColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0).CGColor
                myAlert.alertview.viewText.textColor = UIColor.whiteColor()
                myAlert.alertview.viewText.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
            } else {
                println("Username is available. ")
            }
        } else {
            println("error")
        }
    }

    textField.resignFirstResponder()
    return true
}

现在,当我输入无效字符串并点击下一个文本字段时,会相应地显示警告视图。然而,当我点击返回原始文本字段以更正错误时,问题发生了,警报视图再次显示,这是我不想要的。事实上,如果文本字段中有无效字符串并且我 select 任何按钮或视图上的任何其他交互,警告弹出,即使取消按钮是 selected。我该如何解决这个问题?

问题是为每个文本字段调用了文本字段委托方法。但是您已经编写了 textFieldShouldEndEditing 方法来检查每个文本字段。

修改委托方法,使其仅检查您此时要离开的文本字段。这是相应地更新您的代码的尝试。请注意,我不知道 Swift 所以我可能有一些语法错误,但它应该给你正确的想法。

func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    let acceptedChars = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_")

    var isTaken: Bool = false

    for c in textField.text.utf16 {
        if !acceptedChars.characterIsMember(c) {

            let myAlert = SCLAlertView().showError("Woah There", subTitle: "field can only contain uppercase & lowercase letters A-Z,numbers 0-9, or the special character(s) _", closeButtonTitle: "Got It")
            myAlert.alertview.contentView.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
            myAlert.alertview.circleBG.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
            myAlert.alertview.labelTitle.textColor = UIColor.whiteColor()
            myAlert.alertview.contentView.layer.borderColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0).CGColor
            myAlert.alertview.viewText.textColor = UIColor.whiteColor()
            myAlert.alertview.viewText.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)

            signSecond.userInteractionEnabled = false
            signSecond.highlighted = true

            return false // don't leave since it's invalid

        }else {
            signSecond.userInteractionEnabled = true
            signSecond.highlighted = false
        }
    };

    var query = PFQuery(className: "_User")
    query.whereKey("username", equalTo: textField.text)
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) in
        if error == nil {
            if (objects!.count > 0){
                isTaken = true
                let myAlert = SCLAlertView().showError("Woah There", subTitle: "username \(textField.text) is already taken", closeButtonTitle: "Got It")
                myAlert.alertview.contentView.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
                myAlert.alertview.circleBG.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)
                myAlert.alertview.labelTitle.textColor = UIColor.whiteColor()
                myAlert.alertview.contentView.layer.borderColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0).CGColor
                myAlert.alertview.viewText.textColor = UIColor.whiteColor()
                myAlert.alertview.viewText.backgroundColor = UIColor(red:1.0, green:0.18, blue:0.18, alpha:1.0)

                return false; // invalid, don't leave
            } else {
                println("Username is available. ")
            }
        } else {
            println("error")
        }
    }

    return true
}

关于我上面的代码,没有任何问题需要您自己解决,那就是要解析的查询。目前检查 "username" 值的值是硬编码的。但它应该根据当前文本字段检查字段。

另请注意,当文本字段值无效时,我如何将委托方法更改为 return false。这可以防止用户留下无效的文本字段。