2

Alright - I'm implementing a UIToolbar consisting of a Done button onto a UIKeyboardTypeDecimalPad for a text field.

Problem is, the method that will resign the responder on the text field is not being called. I have put in NSLogs to see what is being called and what isn't, and the setup is called (i.e. UIToolbar initialization, button adding, adding the toolbar to the text field, etc.), but the method isn't ever called when the "Done" button is pressed. In fact, the button doesn't even look "clickable".

- (void) viewDidLoad
{
   UIToolbar *toolbar = [[UIToolbar alloc] init];
   UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePicker:)];
   UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
   [toolbar setItems:@[doneButton, spaceItem]];
   self.textField.inputAccessoryView = toolbar;
}

-(void)donePicker:(id)sender
{
   [self.textField resignFirstResponder];
}

I've attempted to use both [self.textField resignFirstResponder] as well as [self.view endEditing:YES], both bearing no fruit here. Any ideas, folks?

blasthash
  • 98
  • 1
  • 9
  • Where is `toolbar` being added to the view? – Rich Tolley May 31 '14 at 12:38
  • Set the text field's delegate. – nhgrif May 31 '14 at 12:38
  • Okay, so for clarity, the problem here is that the `donePicker:` method isn't called, right? We could eliminate this whole discussion regarding the keyboard, because the actual problem is that the method you're trying to tie to your bar button isn't being called, correct? – nhgrif May 31 '14 at 12:53

1 Answers1

0

Here you simply need to do is :

[yourtextfield setReturnKeyType:UIReturnKeyDone];
yourtextfield.delegate = self;

Now this method will do your work :

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

Enjoy :)

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
  • 1
    I'm using a `DecimalPad` keyboard type. There isn't a return button available. That's why I'm going through the `UIToolbar` route. – blasthash May 31 '14 at 12:49
  • I gave it a whirl; it dismisses the keyboard on simulation if I use the enter key - but as there is no return button, it will not dismiss on the phone. – blasthash May 31 '14 at 12:53