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];
}
Or you can do this (this way you do not care which text field/view it is).
[self.view endEditing:YES];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With