1

I add a UITextField to a table cell dynamically in my app. I'd like to implement a "backgroundClick" method to dismiss the keyboard if the user presses outside the textfield (and outside the keyboard) but I'm unsure how to get a handle on the active keyboard in the backgroundClick method as the dynamic UITextField does not have a defined property to use.

All I know is that it is a UITextField with a particular tag. Is there some way to get a hold of it in code?

Cheers.

alan
  • 187
  • 1
  • 3
  • 12
  • For the benefit of future readers I used the below link offered by Benjamin to fix the problem. I set up a dummy UITextField which I bound to the UITextField I had dynamically added to my grouped table. I was then able to call the following code to dismiss the keyboard at any other point in my code: [dummyField becomeFirstResponder]; [dummyField resignFirstResponder]; – alan Nov 12 '09 at 21:12

2 Answers2

4
UITextField* field =  (UITextField *) [myTableCell viewWithTag: myTag];
[field resignFirstResponder];

Is that what you are seeking?

Edit to reflect comments:

Based on your comment, it's not. So, you probably want to read this other SO question.

Community
  • 1
  • 1
Benjamin Cox
  • 5,880
  • 18
  • 19
  • it's close, the problem is that i don't have a handle on the table cell either. i dynamically populate cells based on a parameter. to one of the cells i add a uitextfield but i wanted to be able to dismiss it with a click outside the cell and outside the keyboard (essentially using a large, invisible button). the problem i have is that the button press event doesn't have information on the cell or the textfield. i'm starting to think that maybe this isn't possible. – alan Nov 11 '09 at 21:31
  • Thanks, exactly what I was after. – alan Nov 12 '09 at 12:04
1

If you have a reference to the UITextField, then you can send resignFirstResponder. That will dismiss the keyboard.

dbachrach
  • 331
  • 2
  • 5