textfieldshouldendediting

textfieldshouldendediting

我实现了此方法,以便在文本字段中输入不必要的字符或已使用的用户名时,向用户发送多个警报视图:

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
}

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

最佳答案

问题是对每个文本字段调用文本字段委托方法。但是您已经编写了textFieldShouldEndEditing方法来检查每个文本字段。
修改delegate方法,使其只检查您当前离开的文本字段。下面是相应地更新代码的尝试。注意,我不知道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”值的值。但它应该基于当前文本字段检查该字段。
还要注意,当文本字段值无效时,我如何更改delegate方法以返回false。这将防止用户留下无效的文本字段。

关于ios - textFieldShouldEndEditing故障?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32041777/

10-09 09:15