views:

135

answers:

2

I'm wondering if there is a way to have the UITextField clear button 'always visible'

textfield.clearButtonMode = UITextFieldViewModeAlways;

doesn't seem to work .. and if it is possible to dismiss the keyboard using the button?

Thanks in advance.

A: 

In your delegate, the function

- (BOOL)textFieldShouldClear:(UITextField *)textField

is called when the users wants to clear the textfield. If you return YES and call

[textField resignFirstResponder];

the keyboard should go away. I don't know about the clearButtonMode, other than that you may want to set it early, preferably before adding the view to its superview.

edit To make sure you really resign the responder, try doing it just a little later:

[textField performSelector:@selector(resignFirstResponder) afterDelay:0.1];
mvds
Thanks, any idea why the keyboard does not go away when calling textFieldShouldClear as above? It works with textFieldShouldReturn for example.
Dan
Yes, might be that after clearing, the UI forces focus to the field anyway. Try doing the resigning a little later, using the line I added to my answer.
mvds
Cheers, seems like the delay solved the problem!
Dan
A: 

UITextFieldDelegate textFieldShouldClear

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

http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear:

Aaron Saunders