swift - Code not working for dismissing Keyboard -
i trying create code dismissing keyboard when touching outside keyboard or when pressing return button. ofcourse not working :d why asking here. how can make work? doesn't give error not responding.
if there simpler solution, welcome provide.
thank time , answer!
here full code:
import uikit class entryviewcontroller: uiviewcontroller, uitextfielddelegate { @iboutlet weak var useremailadresinput: uitextfield! @iboutlet weak var userpasswordinput: uitextfield! @iboutlet weak var userpasswordinputrepeated: uitextfield! @iboutlet weak var usersignup: uibutton! override func viewdidload() { super.viewdidload() self.useremailadresinput.delegate = self self.userpasswordinput.delegate = self self.userpasswordinputrepeated.delegate = self } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } @ibaction func signup(_ sender: any) { let useremail = useremailadresinput.text; let userpassword = userpasswordinput.text; let userpasswordrepeated = userpasswordinputrepeated.text; //hide keyboard upon touch func touchesbegan(_ touches: set<uitouch>, event: uievent?){ self.view.endediting(true) } //hide keyboard upon return key func textfieldshouldreturn(textfield: uitextfield) -> bool { useremailadresinput.resignfirstresponder() return (true) } //check if fields filled in correctly if(useremail?.isempty == true || userpassword?.isempty == true || userpasswordrepeated?.isempty == true){ displayalertmessage(usermessage: "alle velden moeten ingevuld worden"); return; } if(userpassword != userpasswordrepeated){ displayalertmessage(usermessage: "wachtwoorden zijn niet gelijk"); return; } //store data userdefaults.standard.set(useremail, forkey: "useremail"); userdefaults.standard.set(userpassword, forkey: "userpassword"); userdefaults.standard.synchronize(); //signup succesfull var alert = uialertcontroller(title:"succesvol aangemeld!", message: "ga naar je email inbox om je aanmelding te voltooien", preferredstyle: uialertcontrollerstyle.alert); let okaction = uialertaction(title:"ok", style: uialertactionstyle.default, handler: nil); alert.addaction(okaction); self.present(alert, animated: true, completion: nil) } func displayalertmessage(usermessage:string){ var alert = uialertcontroller(title:"alert", message: usermessage, preferredstyle: uialertcontrollerstyle.alert); let okaction = uialertaction(title:"ok", style: uialertactionstyle.default, handler: nil); alert.addaction(okaction); self.present(alert, animated: true, completion: nil) } }
below uitextfield delegate method use resign textfield on press on return button on keyboard.
func textfieldshouldreturn(textfield: uitextfield) -> bool { if textfield == useremailadresinput{ useremailadresinput.resignfirstresponder() } else if textfield == userpasswordinput{ userpasswordinput.resignfirstresponder() } else { userpasswordinputrepeated.resignfirstresponder() } return true }
and put touchesbegan , textfieldshouldreturn method outside signup method.
Comments
Post a Comment