2011-09-29 59 views
0

嗨我想解雇一个数字键盘,它具有自定义的DOne按钮实施。当我尝试关闭它崩溃给下面的错误 *终止应用程序由于未捕获的异常“NSInvalidArgumentException”,理由应用程序:“ - [UIButton的键]:无法识别的选择发送到实例0x7399b40”自定义按钮添加到数字键盘的问题

这里代码段

// 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];被执行。

能有人帮。 感谢 Nachiket

+0

能否请你告诉我什么是 “钥匙”?它是一种方法还是变量?你的按钮试图调用一些名为“key”的选择器。 –

+0

我认为它正在崩溃,因为您正在使用%@在'doneButton:'方法中记录名为'faxTextField'的textField。您不能在日志中打印文本字段。你所能做的就是使用'self.faxTextField.text'打印文本。另外'self.currentResponder'是一个textField,你不能使用%@符号登录。希望这可以帮助你。 –

回答

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

为什么是U使用此日志?

我觉得你应该使用NSLog(@"%@,self.faxTextField.text);

或使用

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

} 
相关问题