Patrick Reck
Patrick Reck

Reputation: 303

XCode - Close keyboard on buttonclick

I just started playing around with XCode a few hours ago, doing the HelloWorld tutorial. I managed to close the keyboard window when the user clicks the Return button.

However, my button should have the exact same behavior as the Return button. It does the same right now, it just doens't close down the keyboard upon submission. How can i do this?

- (IBAction)button:(id)sender {
    [self changeLabelWithText: self.textField.text];

}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == self.textField) {
        [theTextField resignFirstResponder];

        [self changeLabelWithText: self.label.text];
    }
    return YES;
}

- (void)changeLabelWithText: (NSString *) text {
    self.label.text = [NSString stringWithFormat:@"Hej, %@", text];
}

Upvotes: 2

Views: 4849

Answers (3)

Recycled Steel
Recycled Steel

Reputation: 2272

Or you can do this (this way you do not care which text field/view it is).

[self.view endEditing:YES];

Upvotes: 9

Kalpesh
Kalpesh

Reputation: 5334

Try this,

[textFieldName resignFirstResponder];

Upvotes: 6

Deepak
Deepak

Reputation: 1278

- (IBAction)button:(id)sender {
        [TextFieldobj resignFirstResponder];
}

Upvotes: 1

Related Questions