textFieldShouldEndEditing failed?

时间:2015-08-17 01:56:59

标签: ios swift uitextfield alert

我实现了这种方法,当在文本字段中输入无根字符或已经使用的用户名时,向用户发送一些警报视图:

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
}

现在,当我输入无效字符串并点击下一个文本字段时,警报视图会相应显示。但是,当我回到原始文本字段以纠正错误时,问题就出现了,警报视图再次显示,我不想要。事实上,如果文本字段中存在无效字符串,并且我选择任何按钮或视图上交互的任何其他位置,则即使选择了取消按钮,也会弹出警告。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

问题是为每个文本字段调用了文本字段委托方法。但是您已经编写了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
}

上面的代码没有关于你需要自己解决的问题,那就是要解析的查询。目前检查“用户名”值的值是硬编码。但它应该根据当前文本字段检查字段。

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