【发布时间】:2013-02-25 16:44:44
【问题描述】:
我创建了一个自定义单元格,其中包含一个文本字段。我希望键盘在用户按下完成按钮时消失(如下面的屏幕截图所示)。
自定义单元格位于“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];
我想知道是否有办法在按下完成键时隐藏键盘。我的应用程序的屏幕截图如下:
【问题讨论】:
-
我也尝试过该代码,但它不起作用。我不太确定我应该把它放在哪里。 [myTextField resignFirstResponder] 是放在 cellForRowAtIndexPath 方法中还是放在 AccountViewCell 类中?
-
- (IBAction)dismissKeyboard:(id)sender { [aTextBox resignFirstResponder]; }
-
@SulaimanMajeed 请参阅 akash 提供的第二个链接。其他问题已经回答了您的问题。
标签: ios objective-c xcode uikeyboard custom-cell