I have two UITextFields
(e.g. username and password) but I cannot get rid of the keyboard when pressing the return key on the keyboard. How can I do this?
See Managing the Keyboard for a complete discussion on this topic.
When the return key is pressed, call:
[uitextfield resignFirstResponder];
in swift you should delegate UITextfieldDelegate, its important don't forget it, in the viewController, like:
class MyViewController: UITextfieldDelegate{
mytextfield.delegate = self
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
}
}
You can add an IBAction to the uiTextField(the releation event is "Did End On Exit"),and the IBAction may named hideKeyboard,
-(IBAction)hideKeyboard:(id)sender
{
[uitextfield resignFirstResponder];
}
also,you can apply it to the other textFields or buttons,for example,you may add a hidden button to the view,when you click it to hide the keyboard.
Implement the UITextFieldDelegate method like this:
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
Set the Delegate of the UITextField to your ViewController, add a referencing outlet between the File's Owner and the UITextField, then implement this method:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == yourTextField)
{
[textField resignFirstResponder];
}
return NO;
}