Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-30-2008, 09:35 AM   #1 (permalink)
Registered Member
 
NickFalk's Avatar
 
Join Date: Oct 2008
Location: Oslo, Norway
Posts: 20
Default Problem with resignFirstResponder

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.

I have the following files:
(.h)
Code:
#import <UIKit/UIKit.h>

@interface FunViewViewController : UIViewController {
	IBOutlet UITextField *nameField;
	IBOutlet UITextField *numberField;

}
@property (nonatomic, retain) UITextField *nameField;
@property (nonatomic, retain) UITextField *numberField;
-(IBAction) textFieldDoneEditing:(id)sender;
-(IBAction) backgroundClick:(id)sender;

@end
(.m)
Code:
#import "FunViewViewController.h"

@implementation FunViewViewController

@synthesize nameField;
@synthesize numberField;


-(IBAction) textFieldDoneEditing:(id)sender
{
	[nameField resignFirstResponder];
}

-(IBAction) backgroundClick:(id)sender 
{
	[nameField resignFirstResponder]; 
	NSLog(@"Here I am");
}

...

@end
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!
NickFalk is offline   Reply With Quote
Old 11-30-2008, 11:02 AM   #2 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

What object and event is the 'backgroundClick' method bound to?
RickMaddy is offline   Reply With Quote
Old 11-30-2008, 11:25 AM   #3 (permalink)
Registered Member
 
NickFalk's Avatar
 
Join Date: Oct 2008
Location: Oslo, Norway
Posts: 20
Default

Thanks for taking an interest. Not sure I quite follow (which could prove to be a good thing).

Anyway, the backgroundClick is called from a UIButton inside the same view as the nameField (which is a regular UITextField).

The action is called from 'Touch Up Inside' in the button's events.
NickFalk is offline   Reply With Quote
Old 11-30-2008, 12:18 PM   #4 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

I asked because the code you posted didn't show a button so I wasn't sure how the backgroundClick method was ever getting called.
RickMaddy is offline   Reply With Quote
Old 11-30-2008, 12:47 PM   #5 (permalink)
Registered Member
 
NickFalk's Avatar
 
Join Date: Oct 2008
Location: Oslo, Norway
Posts: 20
Default

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.
NickFalk is offline   Reply With Quote
Old 12-01-2008, 12:27 PM   #6 (permalink)
Registered Member
 
NickFalk's Avatar
 
Join Date: Oct 2008
Location: Oslo, Norway
Posts: 20
Default

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.
NickFalk is offline   Reply With Quote
Old 12-01-2008, 12:38 PM   #7 (permalink)
Registered Member
 
Forsworn's Avatar
 
Join Date: Oct 2008
Location: Germany
Posts: 504
Default

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.

Then you add this method:
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	return NO;
}
What you're trying to do is following:
Code:
-(IBAction)backgroundClick {
if (nameField.editing) {
[nameField resignFirstResponder];
}
else {
[numberField resignFirstResponder];
}
}
If it's not working:
Did you add the "shouldEndEditing" method?
If so, post it here.
Forsworn is offline   Reply With Quote
Old 12-01-2008, 01:51 PM   #8 (permalink)
Registered Member
 
NickFalk's Avatar
 
Join Date: Oct 2008
Location: Oslo, Norway
Posts: 20
Default

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?
NickFalk is offline   Reply With Quote
Old 12-01-2008, 05:33 PM   #9 (permalink)
Registered Member
 
NickFalk's Avatar
 
Join Date: Oct 2008
Location: Oslo, Norway
Posts: 20
Default


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...
NickFalk is offline   Reply With Quote
Old 01-04-2009, 03:08 AM   #10 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: Phoenix, AZ
Posts: 71
Default I feel your pain...

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... : )
DanielMedia is offline   Reply With Quote
Old 01-14-2009, 09:28 PM   #11 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 1
Smile Thank you... I feel your pain too.

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?
im3ngs is offline   Reply With Quote
Old 01-21-2009, 01:44 AM   #12 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 1
Default yikes

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.
nickypoo is offline   Reply With Quote
Old 01-21-2009, 12:19 PM   #13 (permalink)
Registered Member
 
NickFalk's Avatar
 
Join Date: Oct 2008
Location: Oslo, Norway
Posts: 20
Default

Thanks guys. I appreciate it. Also find some consolation in the fact that me publishing my (silly) problems can actually be of help to someone.
NickFalk is offline   Reply With Quote
Old 02-12-2009, 05:40 PM   #14 (permalink)
Registered Member
 
Join Date: Feb 2009
Location: Los Angeles
Posts: 40
Send a message via AIM to kallipigous
Default thank you

Same problem here. lesson learned.
kallipigous is offline   Reply With Quote
Old 02-13-2009, 09:56 PM   #15 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 1
Default Thanks a lot, I wasted 2 hours on this

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.
ghamphy is offline   Reply With Quote
Old 02-21-2009, 12:25 AM   #16 (permalink)
New Member
 
Join Date: Feb 2009
Location: Bangkok
Posts: 145
Default

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!
DGYWFT is offline   Reply With Quote
Old 06-11-2009, 06:46 PM   #17 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 28
Talking

Quote:
Originally Posted by NickFalk View Post

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!
xtr33me is offline   Reply With Quote
Old 06-21-2009, 01:29 PM   #18 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 1
Default Same Issue

Quote:
Originally Posted by NickFalk View Post

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!!!
jkriddle is offline   Reply With Quote
Old 06-21-2009, 01:40 PM   #19 (permalink)
Registered Member
 
NickFalk's Avatar
 
Join Date: Oct 2008
Location: Oslo, Norway
Posts: 20
Default

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.
NickFalk is offline   Reply With Quote
Old 07-24-2009, 08:12 PM   #20 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 13
Default

Quote:
Originally Posted by NickFalk View Post

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!
sfdoctorp is offline   Reply With Quote
Old 07-26-2009, 12:15 AM   #21 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 1
Default

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.
Crashspeeder is offline   Reply With Quote
Old 01-10-2010, 11:16 PM   #22 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 1
Default Found the problem in the BOOK

I had the same problem too. (using the same book)

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
JazzyW is offline   Reply With Quote
Old 09-18-2010, 06:08 PM   #23 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 18
Default thank you

Thank you this helped me as well.
aarontra is offline   Reply With Quote
Old 09-23-2010, 05:57 PM   #24 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 1
Default

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?

Thanks
Cathal001 is offline   Reply With Quote
Reply

Bookmarks

Tags
keyboard, resignfirstresonder

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 234
16 members and 218 guests
@sandris, ADY, Alsahir, dacapo, Dani77, djohnson, HemiMG, jansan, JasonR, MarkC, mer10, prchn4christ, ryandb2, smethorst, tomtom100
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:00 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0