2

I have a textfield and when the user clicks on it, they will be presented with the keyboard. there is a GO button on the keyboard, and i want to write an action event to it.

1.) How can i write an action when the user clicks on this button

2.) when the keyboard is open, and when the user clicks on the background i need the keyboard to disappear, how can i do this programatically?

I have no code to demonstrate, i have only added a texfield, so the keyboard would appear by default once clicked

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
sharon
  • 580
  • 12
  • 28

3 Answers3

2

for performing some action on return of textfield/ Go button use following code

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder];

    //call Method when the GO button is pressed   

    return YES;
}

And when user touches on the background and keyboard should return - for that ,write below code

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

[textFiedl resignFirstResponder];

}

hope your issues are resolved with this.

sharon
  • 580
  • 12
  • 28
V.V
  • 3,082
  • 7
  • 49
  • 83
1

A textfield is present whenever a UITextField or UITextView is the first responder. You can manually "show" the keyboard by calling becomeFirstResponder or "hide" it by resignFirstResponder.

In your case, Look at the UITextFieldDelegate reference; when the user hits "GO", the textFieldDidEndEditing: callback is invoked. In this method, you should call resignFirstResponder on the text field to hide the keyboard.

Ash Furrow
  • 12,391
  • 3
  • 57
  • 92
1

For hiding the keyboard when you touch on the background you can write [txtName resignFirstResponder]; where txtName is the reference name of the TextField.

Marijn
  • 10,367
  • 5
  • 59
  • 80
deepti
  • 206
  • 2
  • 15