2011-10-06 42 views
0

我想解僱鍵盤時,用戶按下「取消」UIBarButtonItem。然而,當我點擊取消按鈕時,我得到一個SIGABRT,其中「無法識別的選擇器發送到實例」錯誤。「無法識別的選擇器」錯誤,當試圖解僱鍵盤

我的代碼來創建取消按鈕是:

- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    //Add cancel button to navigation bar 
    UIBarButtonItem *dismissKeyboardBttn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissKeyboard:)]; 
    self.navigationItem.rightBarButtonItem = dismissKeyboardBttn; 
} 

並關閉鍵盤我有這樣的方法:

- (void)dismissKeyboard:(id)sender 
{ 
    [activeField resignFirstResponder]; 
    //^^This line causes the SIGABRT^^ 
} 

似乎很簡單。有任何想法嗎?

更新:activeField只是一個UITextField我用我的scrollView移動到用戶正在編輯的UITextField。它坐落在這兩種方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    activeField = textField; 
} 
- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    activeField = nil; 
} 

更新2:有趣的是,我已經註冊了我的視圖控制器接收鍵盤通知,當我嘗試使用「textFieldShouldReturn」的方法來消除鍵盤,我得到了同樣的錯誤。這是我的textFieldShould返回代碼:

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 

    if ([textField canResignFirstResponder]) 
    { 
     [textField resignFirstResponder]; 
    } 

    return YES; 
} 
+1

你能後,你指定activeField代碼 –

+0

雅,它在那裏現在 – MattL

回答

1

我的情況,請在當前視圖控制器如下:

在頭文件,文本字段創建一個IBAction爲這成爲第一個響應者和調出鍵盤:

- (IBAction)textFieldDidBeginEditing:(UITextField *)textField; 

在實現文件中,創造出創建欄按鈕的方法(在我的情況,「完成」按鈕),並把它添加到的右側navig酒吧。同時,我創建的文本字段之間的目標行動配對(這也成爲第一個響應者

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    // create new bar button with "Done" as text 
    // set the target of the action as the text field (since we want the text field to resign first responder status and dismiss the keyboard) 
    // tell the text field to resign with the stock 'resignFirstResponder' selector 
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                     target:textField 
                     action:@selector(resignFirstResponder)]; 

    // add the button with target/action pairing to the navigation bar 
    [[self navigationItem] setRightBarButtonItem:bbi]; 
} 

此外,如果您希望按鈕消失後,我點擊它(和鍵盤消失),我用的是textFieldDidEndEditing因爲編輯現在已經與第一響應者識別完成:

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    [[self navigationItem] setRightBarButtonItem:nil]; 
} 
1

什麼是activeField?如果它是UIResponder,它應該響應resignFirstResponder。所以也許不是。 UIViews和UIViewControllers是UIResponders。

+0

activeField只是我使用移動的UITextField我。 scrollView到用戶當前編輯的UITextField iting。我在上面設置了代碼。 – MattL

+0

您是否在檢查錯誤時檢查了activeField是否爲零?無編程在大多數情況下默默地忽略選擇器,但我不確定所有。 – morningstar

+0

是的,我沒有檢查,它不是零。它被設置爲我正在編輯的文本字段。我每天都在看這個,不知道什麼是錯的。關於textFieldShouldReturn的一點雖然縮小了。 – MattL

0

晨星是正確的,什麼是activeField,它是一個ID,你可能需要添加一個演員:(UIButton*)?另外,我總是添加此當resignFirstResponder

if(myObject canResignFirstResponder){ 

} 
+0

activeField只是我用來將我的scrollView移動到用戶正在編輯的UITextField的UITextField。我在上面設置了代碼。 – MattL

+0

我還在我的呼叫resignFirstResponder的周圍放置了安全「if」語句,但仍然收到相同的錯誤。 – MattL

+0

MattL通過使用我的電子郵件與我聯繫,所以我可以給你更多支持... – Peres

相關問題