Search code examples
iphoneobjective-cxcodexcode4

NSInvalidArgumentException - touchBackgroundLogin


I'm using a keyboard and I've just implement this method to hide the keyboard when "done" is pressed :

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

Here is the stack trace :

2012-01-10 11:44:33.949 emars[27985:b303] -[ThirdViewController touchBackgroundLogin:]: unrecognized selector sent to instance 0x682fa00
2012-01-10 11:44:33.990 emars[27985:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ThirdViewController touchBackgroundLogin:]: unrecognized selector sent to instance 0x682fa00'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00fb55a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01109313 objc_exception_throw + 44
    2   CoreFoundation                      0x00fb70bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00f26966 ___forwarding___ + 966
    4   CoreFoundation                      0x00f26522 _CF_forwarding_prep_0 + 50
    5   UIKit                               0x002c64fd -[UIApplication sendAction:to:from:forEvent:] + 119
    6   UIKit                               0x00356799 -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x00358c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    8   UIKit                               0x00357a1c -[UIControl touchesBegan:withEvent:] + 277
    9   UIKit                               0x002ead41 -[UIWindow _sendTouchesForEvent:] + 395
    10  UIKit                               0x002cbc37 -[UIApplication sendEvent:] + 447
    11  UIKit                               0x002d0f2e _UIApplicationHandleEvent + 7576
    12  GraphicsServices                    0x01891992 PurpleEventCallback + 1550
    13  CoreFoundation                      0x00f96944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    14  CoreFoundation                      0x00ef6cf7 __CFRunLoopDoSource1 + 215
    15  CoreFoundation                      0x00ef3f83 __CFRunLoopRun + 979

Solution

  • -(IBAction) finEdition: (id)sender is method for Button not for textfield [sender resignFirstResponder];sender is id of Button/ViewController try to resign first responder so application get crashed

     -(IBAction) finEdition: (id)sender
    {
     [self.textfield1 resignFirstResponder]; 
     [self.textfield2 resignFirstResponder]; 
     ...
     ...
     ...
     [self.textfieldn resignFirstResponder]; 
    
    } 
    

    //here this is delegate method of UITextField so you can use [theTextField resignFirstResponder];

    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
        [theTextField resignFirstResponder];
        return YES;
    }
    

    It is your application’s responsibility to dismiss the keyboard at the time of your choosing. You might dismiss the keyboard in response to a specific user action, such as the user tapping a particular button in your user interface. You might also configure your text field delegate to dismiss the keyboard when the user presses the “return” key on the keyboard itself. 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.

    So, you have to send resignFirstResponder somehow. But there is a possibility that textfield loses focus another way during processing of textFieldShouldReturn: message. This also will cause keyboard to disappear.
    Make sure you're setting your view/viewcontroller to be the UITextField's delegate after you init the textfield in the .m:

    yourTextField.delegate = self;