3

I have a pick list to select a day and a text field to show the selected date. It will be like this...

alt text

If i choose any date, it will be like this....

alt text

The cross symbol in text field is acheived by the code....

textField.clearButtonMode=UITextFieldViewModeAlways;

Now my problem is, while clicking on this cross button, a keyboard was displayed. This is like....

alt text

But i want the cross button only for erase the text field. The keyboard should not come. Is it possible?

braX
  • 11,506
  • 5
  • 20
  • 33

4 Answers4

3

In the textfield's 'Editing did begin' method, add the following:

[UITextField resignFirstResponder];

With this in place, that keyboard won't show up.

Happy coding :)

Aurum Aquila
  • 9,126
  • 4
  • 25
  • 24
  • This is the wrong place to do that. You may end up with the keyboard animation showing and then disappearing. The right place to do that is before the text field become active, and so the keyboard won't be shown, even for few instants. – vfn Jan 24 '11 at 06:28
  • Good point vfn. I didn't think about it a whole lot, as the code worked on my iPhone 4 when I quickly tested it. – Aurum Aquila Jan 24 '11 at 06:38
3

On your UITextFieldDelegate, implement the method - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField and return NO;

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
vfn
  • 6,026
  • 2
  • 34
  • 45
1

Try setting the editable property of that textfield to false.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
0

As others have said, you can hide the keyboard through the UITextFieldDelegate protocol and through a [texfField resignFirstResponder] method. Alternatively, as vfn suggested, you can prevent thE keyboard from showing altogether.

For that button though, you are young to want to set the clearButtonMode property of the text field. To see what your available options are, read this: http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html#//apple_ref/doc/c_ref/UITextFieldViewMode

Moshe
  • 57,511
  • 78
  • 272
  • 425
  • `UITextFieldDelegate` is a protocol not a class. To resign as first responder, you should be notified that the text field became active, and for that you have to implement the protocol, and so there is no OR. – vfn Jan 24 '11 at 06:25
  • @vfn I stand corrected. It's not early in my time zone. Thanks for pointing that out. – Moshe Jan 24 '11 at 06:34