我已經創建了一個包含文本字段的自定義單元格。當用戶按下完成按鈕時,我希望鍵盤消失(如下面的屏幕截圖所示)。如何使完成按鈕返回鍵盤
自定義單元格位於「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];
我想知道是否有辦法隱藏鍵盤時,按下完成鍵。我的應用程序的屏幕快照所示:
提到這個問題:http://stackoverflow.com/questions/6906246/how-do-i-dismiss-the-ios-keyboard – 2013-02-25 16:47:57
我也試過這個代碼,但它不起作用。我不太確定我應該把它放在哪裏。 [myTextField resignFirstResponder] 我會把它放到cellForRowAtIndexPath方法或AccountViewCell類中嗎? – 2013-02-25 16:50:58
- (IBAction)dismissKeyboard:(id)sender {aTextBox resignFirstResponder]; } – 2013-02-25 16:55:55