0
votes

Hi I am trying to dismiss a numpad-keyboard which has a custom DOne Button implemented. When I try to dismiss it the app it crashes giving the following error * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton key]: unrecognized selector sent to instance 0x7399b40'

Here is the code snippet

// Snippet
  - (void)keyboardWillShow:(NSNotification *)notification {  
      // create custom button
     doneButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
     doneButton.frame = CGRectMake(0, 163, 106, 53);
     doneButton.adjustsImageWhenHighlighted = NO;
     [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"]       forState:UIControlStateNormal];
     [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
     [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];


    // locate keyboard view
  UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;    
UITextField * tempTextField = (UITextField*)self.currentResponder;   //Current responder is the textfield which is clicked


for(int i=0; i<[tempWindow.subviews count]; i++) {
    keyboard = [tempWindow.subviews objectAtIndex:i];

    if([[[keyboard class]description] hasPrefix:@"UIPeripheralHostView"])
    {

        if(tempTextField.keyboardType == UIKeyboardTypeNumberPad)  
        {
            UIView *viewToInsertButtonInto = [[[[[[[[keyboard subviews] objectAtIndex:0] subviews]objectAtIndex:0]subviews]objectAtIndex:0]subviews]objectAtIndex:0];
            [viewToInsertButtonInto addSubview:self.doneButton];
        }

    }

}

}

- (void)doneButton:(id)sender
{
    NSLog(@"%@,%@  _________     %s",self.faxTextField,self.currentResponder,__FUNCTION__);
   UITextField * tempTextField = (UITextField*)self.currentResponder;
   if ([tempTextField isFirstResponder]) {
    NSLog(@"It is the first responder");
}
  [tempTextField resignFirstResponder];
}

The App Crashes when [tempTextField resignFirstResponder]; is executed.

Can someone please help. Thanks Nachiket

1
Can you please tell me what is "key"? Is it a method or a variable? Your button is trying to call some selector called "key". - Parth Bhatt
I think it is crashing because you are using a %@ for Logging a textField called faxTextField in your doneButton: method. You cannot print a textField in a Log. All you can do is print its text using self.faxTextField.text. Also self.currentResponder is a textField which you cannot log using a %@ sign. Hope this helps you. - Parth Bhatt

1 Answers

1
votes
NSLog(@"%@,%@  _________     %s",self.faxTextField,self.currentResponder,__FUNCTION__);

why are u using this log?.

I think u should use NSLog(@"%@,self.faxTextField.text);

or use

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;

}