Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 29, 2008, 07:00 PM   #1
NickFalk
macrumors 6502
 
Join Date: Jun 2004
Problem calling resignFirstResponder


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?
NickFalk is offline   0 Reply With Quote
Old Nov 29, 2008, 09:26 PM   #2
tacoman667
macrumors regular
 
Join Date: Mar 2008
Quote:
Originally Posted by NickFalk View Post

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?
tacoman667 is offline   0 Reply With Quote
Old Nov 30, 2008, 05:50 AM   #3
NickFalk
Thread Starter
macrumors 6502
 
Join Date: Jun 2004
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 is offline   0 Reply With Quote
Old Nov 30, 2008, 07:56 AM   #4
NickFalk
Thread Starter
macrumors 6502
 
Join Date: Jun 2004
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
NickFalk is offline   0 Reply With Quote
Old Dec 1, 2008, 07:14 AM   #5
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: 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");
}
caveman_uk is offline   0 Reply With Quote
Old Dec 1, 2008, 07:53 AM   #6
NickFalk
Thread Starter
macrumors 6502
 
Join Date: Jun 2004
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 is offline   0 Reply With Quote
Old Dec 1, 2008, 12:23 PM   #7
NickFalk
Thread Starter
macrumors 6502
 
Join Date: Jun 2004
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:
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.
?
NickFalk is offline   0 Reply With Quote
Old Dec 1, 2008, 12:31 PM   #8
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: 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.
dejo is offline   0 Reply With Quote
Old Dec 1, 2008, 01:55 PM   #9
NickFalk
Thread Starter
macrumors 6502
 
Join Date: Jun 2004
Makes sense Dejo, but then the Apress book claims the following:
Quote:
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?
NickFalk is offline   0 Reply With Quote
Old Dec 1, 2008, 02:01 PM   #10
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by NickFalk View Post
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");
}
?
dejo is offline   0 Reply With Quote
Old Dec 1, 2008, 02:50 PM   #11
NickFalk
Thread Starter
macrumors 6502
 
Join Date: Jun 2004
Thanks. I like the way you think. Makes sense, but unfortunately Xcode doesn't seem to agree...
NickFalk is offline   0 Reply With Quote
Old Dec 1, 2008, 03:04 PM   #12
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by NickFalk View Post
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).
dejo is offline   0 Reply With Quote
Old Dec 1, 2008, 05:32 PM   #13
NickFalk
Thread Starter
macrumors 6502
 
Join Date: Jun 2004
Egg in my face...


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   0 Reply With Quote
Old Jun 9, 2009, 10:06 AM   #14
Milner99
macrumors newbie
 
Join Date: May 2009
Location: 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!
Milner99 is offline   0 Reply With Quote
Old Jun 9, 2009, 12:17 PM   #15
justfred
macrumors newbie
 
Join Date: May 2009
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...
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.
justfred is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: iPhone 5c speaker problem when calling iPodJedi iPhone 10 Dec 16, 2013 10:29 PM
5c wi-fi calling? kenscanon iPhone 10 Oct 6, 2013 12:42 PM
5S have Wi-Fi Calling? dbalone iPhone 4 Sep 11, 2013 02:01 PM
Calling only iPhone appleva iPhone 25 Nov 11, 2012 08:06 AM

Forum Jump

All times are GMT -5. The time now is 11:31 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC