【发布时间】:2011-09-29 04:40:01
【问题描述】:
您好,我正在尝试关闭已实现自定义 DOne 按钮的数字键盘。当我尝试关闭它时,应用程序崩溃并出现以下错误 * 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[UIButton key]: unrecognized selector sent to instance 0x7399b40'
这里是代码sn-p
// 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];
}
当 [tempTextField resignFirstResponder]; 时应用程序崩溃;被执行。
有人可以帮忙吗? 谢谢 纳奇基特
【问题讨论】:
-
你能告诉我什么是“key”吗?是方法还是变量?您的按钮正在尝试调用一些名为“key”的选择器。
-
我认为它崩溃了,因为您在
doneButton:方法中使用 %@ 来记录一个名为faxTextField的文本字段。您不能在日志中打印文本字段。您所能做的就是使用self.faxTextField.text打印其文本。self.currentResponder也是一个文本字段,您不能使用 %@ 符号记录它。希望这对您有所帮助。