Problem with Custom Button added to numpad
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 开发者_如何学Pythonto 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
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;
}
精彩评论