Hi there. I'm ploughing through "Beginning iPhone Development" from Apress and thought I had started to get to grips with the basics. Unfortunately I seem to have been mistaken.
I'm trying to close the keyboard with a UIButton (in addition to the "Done") field. (The idea is to eventually make this into an invisible "background" button allowing the user to close any keyboard when tapping outside the fields.
Now, the [nameField resignFirstResponder] works fine when it's called in the textFieldDoneEditing action (which is called directly from the 'Did End On Exit' in the IB.
When calling the [nameField resignFirstResponder] in the backgroundClick action it simply doesn't work. I know the action itself is called OK as the debugger does return the "Here I am" string.
It's probably some basic concept that I've missed completely, but that's the life of a newbie. Any help would be highly appreciated! Cheers!
Ah, I see. No the action is getting called as I can clearly see the "Here I am" string showing up in the debugger console every time I click the button.
Thanks though, all ideas are extremely welcome at this point as I'm completely at a loss.
I've been investigating and through the feedback I got at another forum I've come to the conclusion that nameField might actually not be registered as FirstResponder, this is even true when calling an action directly from the keyboard using the "Did End On Exit" event.
Admittedly I'm a complete beginner at this stuff but this is what I found in Apple's own documentation:
Quote:
...When the user taps in a text field, that text field becomes the first responder...
Yet, somehow this is apparently no longer the case even when the keyboard is open and I click the button I've created. This seems even stranger when I read the following:
Quote:
To dismiss the keyboard, send the resignFirstResponder message to the text field that is currently the first responder. Doing so causes the text field object to end the current editing session (with the delegate object’s consent) and hide the keyboard.
This is exactly what I'm trying to achieve, yet I'm unable to get it to work at all.
If you want to close the keboard when the user pressed the "DONE" button you'll have to register your view controller as a delegate of your textfields.
Thank Forsworn, sorry if I'm being thick but I'm going bonkers.
I already had the "done" button working by calling an [sender resignFirstResponder] on the 'Did End On Exit' event. However, as I've come to suspect that the problem stems from the active text-field not being firstResponder I did some experiments and inserted an 'if' to deliver a prompt in the debugger if the textField was first responder.
Code:
if ([nameField isFirstResponder])
...
Now the prompt never fired which is something I find really confusing. I've since found that the keyboard automatically closes when using the "done" button even without the resignFirstResponder in the IBAction it is hooked up to.
I've tried to make the File's Owner a delegate of the textFields in the IB, but the functionality has not changed. The Apress book even claims:
Quote:
When a text field yields first responder status, the keyboard associated with it goes away...
Now, this could very well be erroneous but if it is not, then clearly the active textField must be first responder? And if this is the case clearly the resignFirstResponder call should work?
OK, my bad, my bad, my bad. (Did I mention it's my bad?)
As way to often with these sort of things it was a seemingly small omission on my part that caused all the problems.
I figured I'd better go through all the IB outlets and action calls. And there it was staring me in my egg-covered, blushing mug: I had forgotten to connect the nameField and numberField outlets from the 'files owner' to the actual textFields.
Well, at least the problem was solved. Sorry for your time people...
I had the same issue and went back and forth for hours trying to figure out what in the code was wrong (I'm reading the same book btw). Turns out it was the same issue you had. Somehow I mucked up the outlets in Interface Builder. After reading your post, I checked my outlets and HALLELUJAH!!... It worked... : )
I too have been beating my head and pulling my hair over this... I rewrote the application three times going by the book... I don't recall it actually explicitly saying to do so. As a beginner, I think it would be helpful to have this more clear, agreed?
If it's any consolation on your self-reflection of your incompetence at the simplest of examples -- I have spent the last hour on this as well(!). But hey, you can bet that at least we won't make this mistake again. ;-)
PS -- Thanks for asking the question, I have a feeling I would have spent at least another hour banging my head against a wall.
The book never mentions to do that.. what crap. thanks to you I cracked it. like the others said, I wasted a lot of time re-doing this a couple of times , thinking I was missing something.
Ahh, I just had the same problem! ha. I had everything working fine, keyboard closing, but for some reason my text would never send anything after clicking DONE. After reading this, I made sure file owner was connected to outlet on textfield. Boom, that solved it!
Thanks for asking this and mentioning what you missed!
OK, my bad, my bad, my bad. (Did I mention it's my bad?)
As way to often with these sort of things it was a seemingly small omission on my part that caused all the problems.
I figured I'd better go through all the IB outlets and action calls. And there it was staring me in my egg-covered, blushing mug: I had forgotten to connect the nameField and numberField outlets from the 'files owner' to the actual textFields.
Well, at least the problem was solved. Sorry for your time people...
OMG...I can't thank you enough for posting this lol. When I read your post I smacked myself in the head cause I knew I never connected my controls either. Sure enough, I connected them up and it worked like a charm! Thanks again!
OK, my bad, my bad, my bad. (Did I mention it's my bad?)
As way to often with these sort of things it was a seemingly small omission on my part that caused all the problems.
I figured I'd better go through all the IB outlets and action calls. And there it was staring me in my egg-covered, blushing mug: I had forgotten to connect the nameField and numberField outlets from the 'files owner' to the actual textFields.
Well, at least the problem was solved. Sorry for your time people...
AAARGH same problem as everyone else - not connecting the outlets. Definitely not mentioned in the book I'm using. THANK YOU!!!
Don't think I've ever experienced so many people being grateful for one of my mistakes. :-) Maybe I should inform Apress so they could look into clarifying the matter for their next edition.
OK, my bad, my bad, my bad. (Did I mention it's my bad?)
As way to often with these sort of things it was a seemingly small omission on my part that caused all the problems.
I figured I'd better go through all the IB outlets and action calls. And there it was staring me in my egg-covered, blushing mug: I had forgotten to connect the nameField and numberField outlets from the 'files owner' to the actual textFields.
Well, at least the problem was solved. Sorry for your time people...
my friend, you are a genius. i too did the same exact thing. the non-sense is that this all happened, when i was re-arranging the new hidden button. in the process of adding it and moving it, the link was somehow detached!!! everything was working along - if I had only re-tested it, I would have seen it!
I'm terribly sorry if I'm making the same mistake as everyone else but I've read through the posts at least 10-15 times and I'm not entirely certain I'm making the same mistake. I'm not working off of a book, you see. I simply found a tutorial online and wanted to expand upon it. I wanted to have the keyboard close on a UIButton press, as do all of you.
Here's my issue:
I have 2 text fields and a "Did End On Exit" for each. I've tried setting their delegates to the UIButton, the File's Owner, the First Responder, and to no avail. With the File's Owner and First Responder I can select my textFieldShouldReturn function but even though it fires (and the console shows that it's firing on button press AND return) only the return button on the keyboard closes the keyboard. I tried setting a Did End On Exit for the button as well. I'm not sure what I've done wrong. All help is appreciated.
I found the problem on the book, it simplifies the steps by just asking us "Connect the outlets", but didn't specifically point out "File's Owner connect to nameField". Newbie like me will easily miss out this tiny details.
"Connecting Outlets
.....Control-drag from File's Owner to each of the text fields, and connect them to their corresponding outlets. ....." ~Apress
Hi, having what I think is a similar problem - when I click 'Done' the app exits back to the simulator home screen rather than just closing the keyboard. Am I missing something obvious - thought I had followed the book correctly?