gpt4 book ai didi

ios - textFieldShouldEndEditing 故障?

转载 作者:行者123 更新时间:2023-11-28 13:08:32 31 4
gpt4 key购买 nike

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

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
}

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

最佳答案

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

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

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

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

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com