2013-02-25 116 views
0

我已經創建了一個包含文本字段的自定義單元格。當用戶按下完成按鈕時,我希望鍵盤消失(如下面的屏幕截圖所示)。如何使完成按鈕返回鍵盤

自定義單元格位於「AccountViewCell」中。在我的代碼,我打電話,這顯示自定義單元格:

- (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     static NSString *CellIdentifier = @"AccessCard"; 
     static NSString *Cellnib = @"AccountViewCell"; 

     AccountViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 

      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:Cellnib owner:self options:nil]; 
      cell = (AccountViewCell *)[nib objectAtIndex:3]; 
     } 

     cell.data.text = [tableData objectAtIndex:indexPath.row]; 

     return cell; 
    } 

    if (indexPath.section == 1) 
    { 
     static NSString *CellIdentifier = @"Password"; 
     static NSString *Cellnib = @"AccountViewCell"; 

     AccountViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 

      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:Cellnib owner:self options:nil]; 
      cell = (AccountViewCell *)[nib objectAtIndex:4]; 
     } 

     cell.data.text = [tableData objectAtIndex:indexPath.row]; 

     return cell; 
    } 

    return 0; 
} 

用戶能夠輸入文字,但我似乎無法使鍵盤消失。

我也曾在AccountViewCell創建了一個方法來隱藏鍵盤:

- (void)textfieldInput 
{ 
    UIToolbar* padToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; 
    padToolBar.barStyle = UIBarStyleBlackTranslucent; 

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
            initWithTitle:@"Done" 
            style:UIBarButtonItemStyleDone 
            target:self 
            action:@selector(doneWithPad)]; 
    [doneButton setWidth:65.0f]; 

    padToolBar.items = [NSArray arrayWithObjects: 
         [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelPad)], 
         [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
         doneButton, 
         nil]; 

    [padToolBar sizeToFit]; 
    textField.inputAccessoryView = padToolBar; 

} 

但是,當我把它在的cellForRowAtIndexPath,這是行不通的。

AccountViewCell* keyboard = [[AccountViewCell alloc] init]; 
[keyboard textfieldInput]; 

我想知道是否有辦法隱藏鍵盤時,按下完成鍵。我的應用程序的屏幕快照所示:

keyboard does not disappear

+0

提到這個問題:http://stackoverflow.com/questions/6906246/how-do-i-dismiss-the-ios-keyboard – 2013-02-25 16:47:57

+0

我也試過這個代碼,但它不起作用。我不太確定我應該把它放在哪裏。 [myTextField resignFirstResponder] 我會把它放到cellForRowAtIndexPath方法或AccountViewCell類中嗎? – 2013-02-25 16:50:58

+0

- (IBAction)dismissKeyboard:(id)sender {aTextBox resignFirstResponder]; } – 2013-02-25 16:55:55

回答

1

。在完成按鈕的操作方法使用此一行代碼:

[myTextField resignFirstResponder];

+0

「讓我的控制委託給我的班級」是什麼意思?我會如何去做這件事?然後,我會將這一行代碼放入我的CellForRowAtIndexPath方法或我的AccountViewCell類中嗎? – 2013-02-25 16:54:21

+0

在你完成的按鈕的操作方法中放入這行代碼。 – 2013-02-25 16:55:34

1

Inlucde UITextFeildDelegate方法#.h文件中

提供[textfeild setDelegate:self];

[textField setReturnKeyType:UIReturnKeyDone]; 

包括

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
[textField resignFirstResponder]; 
} 
+0

當文本字段不再是第一響應者時,調用'textFieldDidEndEditing:'委託方法。所以在這個方法中調用'resignFirstResponder'是沒有意義的。當按下「返回」鍵時,這些都不會被調用。 – rmaddy 2013-02-25 17:47:26

+0

檢查我的答案。它不幸地顯示了這種方法。 – Madhu 2013-02-26 05:21:46

0

必須impliment在頭文件中的UITextViewDelegate,然後設置你的文本視圖委託是你的委託。通常,一旦您的代理設置,你可以使用相應的委託方法你會做這一切在同一類文件,這樣就可以做

aTextField.delegate = self; 

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textView resignFirstResponder]; 
} 
+0

這不是處理在文本字段中按下的「返回」鍵的正確委託方法。 – rmaddy 2013-02-25 17:48:15

0

當連接在你的UITextField。 xib文件,將目標連接到DidEndOnExit。

相關問題