views:

785

answers:

2

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.

+1  A: 
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.

Benjamin Cox
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
See my edit above...
Benjamin Cox
Thanks, exactly what I was after.
alan
Cool - how about "accepting" the answer formally?
Benjamin Cox
Sorry Benjamin, didn't know about answer acceptance. Better late than never I hope.
alan
+1  A: 

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

dbachrach