Jovilla
Jovilla

Reputation: 1

iPhone: How do I create a simple Done button?

Newbie alert! I have a simple calculator with 4 text input fields. When I tap into a field the number pad appears and I enter numbers. No problems so far. Now, when testing this using the simulator I press return on the keyboard and using TextFieldShouldReturn my fields perform their calculations perfectly and the number keypad disappears nicely. The problem is, the number keypad does not have a 'Done' key so if I place a button let's say on the toolbar how do I code it to perform the action of the Return key?

Upvotes: 0

Views: 531

Answers (1)

kubi
kubi

Reputation: 49384

How do you dismiss the keyboard when editing a UITextField

I asked the same question. :-)

the answer is that you set the delegate for the UITextField to your controller and impliment this method.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

In response to your clarification

- (void)doneButtonPressed:(id)sender {
  [_textField resignFirstResponder];
  [self performCalculations];
}

You'll have to move all your calculations into the performCalculatinos method and make sure you have a variable that points to the UITextField.

Upvotes: 1

Related Questions