Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

NickFalk

macrumors 6502
Original poster
Jun 9, 2004
347
1
:(
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:
Code:
-(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

macrumors regular
Mar 27, 2008
143
0
:(
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:
Code:
-(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

macrumors 6502
Original poster
Jun 9, 2004
347
1
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

macrumors 6502
Original poster
Jun 9, 2004
347
1
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?

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
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
When you click the button is the TextField still the firstResponder - it probably isn't. Check in action method for the button with
Code:
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

macrumors 6502
Original poster
Jun 9, 2004
347
1
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

macrumors 6502
Original poster
Jun 9, 2004
347
1
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors 6502
Original poster
Jun 9, 2004
347
1
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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:
Code:
-(IBAction) backgroundClick:(id)sender 
{
	[sender resignFirstResponder];
	[nameField resignFirstResponder]; 
	NSLog(@"Here I am");
}
?
 

NickFalk

macrumors 6502
Original poster
Jun 9, 2004
347
1
Thanks. I like the way you think. Makes sense, but unfortunately Xcode doesn't seem to agree...
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors 6502
Original poster
Jun 9, 2004
347
1
Egg in my face...

:eek:
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

macrumors newbie
May 27, 2009
5
0
London, UK
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

macrumors newbie
May 8, 2009
21
0
:eek:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.