sharon
sharon

Reputation: 580

Adding and removing keyboard

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

Upvotes: 2

Views: 187

Answers (3)

V.V
V.V

Reputation: 3094

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.

Upvotes: 2

deepti
deepti

Reputation: 206

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

Upvotes: 1

Ash Furrow
Ash Furrow

Reputation: 12421

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.

Upvotes: 1

Related Questions