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
能否請你告訴我什麼是 「鑰匙」?它是一種方法還是變量?你的按鈕試圖調用一些名爲「key」的選擇器。 –
我認爲它正在崩潰,因爲您正在使用%@在'doneButton:'方法中記錄名爲'faxTextField'的textField。您不能在日誌中打印文本字段。你所能做的就是使用'self.faxTextField.text'打印文本。另外'self.currentResponder'是一個textField,你不能使用%@符號登錄。希望這可以幫助你。 –