ios - textFieldShouldEndEditing malfunction? -
i implemented method send number of alert views user when unwarranted character, or taken username inputed in textfields:
func textfieldshouldendediting(textfield: uitextfield) -> bool { let acceptedchars = nscharacterset(charactersinstring: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890_") var istaken: bool = false c in usernametxt.text.utf16 { if !acceptedchars.characterismember(c) { let myalert = sclalertview().showerror("woah there", subtitle: "username can contain uppercase & lowercase letters a-z,numbers 0-9, or 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 } }; b in passwordtxt.text.utf16 { if !acceptedchars.characterismember(b) { let myalert = sclalertview().showerror("woah there", subtitle: "password can contain uppercase & lowercase letters a-z,numbers 0-9, or 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 } }; in confirmtxt.text.utf16 { if !acceptedchars.characterismember(a) { let myalert = sclalertview().showerror("woah there", subtitle: "password confirmation can contain uppercase & lowercase letters a-z,numbers 0-9, or 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) 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 available. ") } } else { println("error") } } textfield.resignfirstresponder() return true }
now when input invalid string , tap next textfield, alert view shows accordingly. issue happens when tap original textfield correct mistake, alert view shows once again, not want. matter fact, if there invalid string within textfield , select button or anywhere else thats interactive on view, warnings pop up, if cancel button selected. how fix this?
the problem text field delegate method called each text field. have written textfieldshouldendediting
method check every text field.
modify delegate method checks text field leaving @ moment. here's attempt update code accordingly. note don't know swift may have syntax wrong should give right idea.
func textfieldshouldendediting(textfield: uitextfield) -> bool { let acceptedchars = nscharacterset(charactersinstring: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890_") var istaken: bool = false c in textfield.text.utf16 { if !acceptedchars.characterismember(c) { let myalert = sclalertview().showerror("woah there", subtitle: "field can contain uppercase & lowercase letters a-z,numbers 0-9, or 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) 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 available. ") } } else { println("error") } } return true }
there not thing code above need solve on own , query parse. hardcode check value of "username" value. should check field based on current text field.
also note how changed delegate method return false
when text field value isn't valid. prevents user leaving invalid text field.
Comments
Post a Comment