Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}
like image 398
Patrick Reck Avatar asked Jan 31 '26 15:01

Patrick Reck


1 Answers

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

[self.view endEditing:YES];
like image 104
Recycled Steel Avatar answered Feb 02 '26 18:02

Recycled Steel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!