PDA

View Full Version : Problem calling resignFirstResponder




NickFalk
Nov 29, 2008, 06:00 PM
:(
Just got the 'Beginning iPhone Development' from Apress. I believed I was starting to wrap my head around the basics, but for some reason I can't get the following code to work:
-(IBAction) textFieldDoneEditing:(id)sender
{
[nameField resignFirstResponder];
}

-(IBAction) backgroundClick:(id)sender
{
[nameField resignFirstResponder];
NSLog(@"Here I am");
}
The first action works fine and closes the Keyboard as it should. The second action is hooked up to a button but won't perform the Keyboard-close. I know the action is called as the debugger returns the "Here I am" statment...

Anyone?



tacoman667
Nov 29, 2008, 08:26 PM
:(
Just got the 'Beginning iPhone Development' from Apress. I believed I was starting to wrap my head around the basics, but for some reason I can't get the following code to work:
-(IBAction) textFieldDoneEditing:(id)sender
{
[nameField resignFirstResponder];
}

-(IBAction) backgroundClick:(id)sender
{
[nameField resignFirstResponder];
NSLog(@"Here I am");
}
The first action works fine and closes the Keyboard as it should. The second action is hooked up to a button but won't perform the Keyboard-close. I know the action is called as the debugger returns the "Here I am" statment...

Anyone?


I know that the readme stated that you cannot resignFirstResponder ion a UITextView when it is not visible. Is this where you are having the issue?

NickFalk
Nov 30, 2008, 04:50 AM
Thanks.
Unfortunately this is not the case. The state of the program is identical when trying to call both IBActions. Despite this, the [nameField resignFirstResponder]; simply won't work when performing the backgroundClick action.

NickFalk
Nov 30, 2008, 06:56 AM
While I still can't figure out my mistake it seems to me that the problem is probably that the backgroundClick action does not recognize the nameField perhaps there's an error in my .h-file?

#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

caveman_uk
Dec 1, 2008, 06:14 AM
When you click the button is the TextField still the firstResponder - it probably isn't. Check in action method for the button with


if ([nameField isFirstResponder])
[nameField resignFirstResponder];
else {
[nameField becomeFirstResponder]; // not sure this will actually work but I'd try it
[nameField resignFirstResponder];
NSLog (@"nameField wasn't first responder");
}

NickFalk
Dec 1, 2008, 06:53 AM
OK, now we're getting somewhere. While I am still unable to close the keyboard clicking the button you were right in your suspicion that nameField wasn't the firstResponder.

I was not able to force nameField to become firstResponder and this wouldn't be quite as straightforward anyway, as I actually want the action to close one of two possible keyboards (one for a numberField as well).

Thank you though, you're the first to actually figure out the cause of the problem. Certainly a huge step forward. :)

NickFalk
Dec 1, 2008, 11:23 AM
And it seems to be getting stranger:
I've found that even when calling an action directly from the keyboard using the "Did End On Exit" event the nameField is not the FirstResponder(!)

Admittedly I'm a complete beginner at this stuff but this is what I found in Apple's own documentation:
...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:
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.
?

dejo
Dec 1, 2008, 11:31 AM
I believe this is because your button has now become the first responder and the text field has now been pushed down to be the second responder. My understanding is that there is a stack of responders. When you click the textfield, it gets pushed onto the stack and becomes the first responder. When you resign the first responder, it get popped off the stack. But in your case, by clicking this other button, you have just pushed it onto the responder stack. So, you may need to find a way to resign it as first responder and then trigger another resign for the text field.

NickFalk
Dec 1, 2008, 12:55 PM
Makes sense Dejo, but then the Apress book claims the following:
When a text field yields first responder status, the keyboard associated with it goes away...
If this is correct then clearly the text field must still be first responder?

dejo
Dec 1, 2008, 01:01 PM
If this is correct then clearly the text field must still be first responder?
Not if the textfield didn't have first responder status to begin with.

Can you try the following method instead:
-(IBAction) backgroundClick:(id)sender
{
[sender resignFirstResponder];
[nameField resignFirstResponder];
NSLog(@"Here I am");
}
?

NickFalk
Dec 1, 2008, 01:50 PM
Thanks. I like the way you think. Makes sense, but unfortunately Xcode doesn't seem to agree...

dejo
Dec 1, 2008, 02:04 PM
Thanks. I like the way you think. Makes sense, but unfortunately Xcode doesn't seem to agree...
Oh, well. Was worth a shot, eh? Well, I'll try to throw together a small test case later today so see if I can try to come up with something. (I may need similar functionality at some point in the future myself).

NickFalk
Dec 1, 2008, 04:32 PM
:o
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...

Milner99
Jun 9, 2009, 09:06 AM
I had this EXACT same problem, and I'm guessing I'm learning from the same book you are as my code snippet is the same also.

Thanks for posting your reply - it sorted my issue as well.

Finding it a struggle moving from .NET to Cocoa/Obj-C. Glad I am though!

justfred
Jun 9, 2009, 11:17 AM
:o
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...

Reading through the thread, this was my first guess. The text fields all need to be linked in two directions. One as the outlet of the view, and the other being the view as the delegate to each text field... pretty much without fail.