1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Problem calling resignFirstResponder

Discussion in 'iPhone/iPad Programming' started by NickFalk, Nov 29, 2008.

  1. macrumors 6502

    NickFalk

    #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?
     
  2. macrumors regular

    #2

    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?
     
  3. macrumors 6502

    NickFalk

    #3
    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.
     
  4. macrumors 6502

    NickFalk

    #4
    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
     
  5. Guest

    caveman_uk

    #5
    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");
    }
    
     
  6. macrumors 6502

    NickFalk

    #6
    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. :)
     
  7. macrumors 6502

    NickFalk

    #7
    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:
    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:
    ?
     
  8. Moderator

    dejo

    Staff Member

    #8
    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.
     
  9. macrumors 6502

    NickFalk

    #9
    Makes sense Dejo, but then the Apress book claims the following:
    If this is correct then clearly the text field must still be first responder?
     
  10. Moderator

    dejo

    Staff Member

    #10
    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");
    }
    ?
     
  11. macrumors 6502

    NickFalk

    #11
    Thanks. I like the way you think. Makes sense, but unfortunately Xcode doesn't seem to agree...
     
  12. Moderator

    dejo

    Staff Member

    #12
    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).
     
  13. macrumors 6502

    NickFalk

    #13
    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...
     
  14. macrumors newbie

    #14
    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!
     
  15. macrumors newbie

    #15
    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.
     

Share This Page