如何在完成按钮上创建键盘返回
如何在完成按钮上创建键盘返回
我已经创建了一个包含文本字段的自定义单元格。当用户按下完成按钮时,我希望键盘消失(如下面的屏幕截图所示)。
自定义单元格位于"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:@"完成"
style:UIBarButtonItemStyleDone
target:self
action:@selector(doneWithPad)];
[doneButton setWidth:65.0f];
padToolBar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"取消" 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];
To make the keyboard return on the done button, you need to add the above line of code in the action method of the Done button. This code resigns the first responder status of the text field, which means that the keyboard will be dismissed.
The phrase "Make my control's delegate to my class" refers to setting the delegate property of the text field to the class that handles its events and actions. To do this, you can assign the delegate property of the text field to the instance of your class.
You can put the one line of code mentioned above in the action method of the Done button. This action method could be in the CellForRowAtIndexPath method or in the AccountViewCell class, depending on your specific implementation.
By adding this code to the Done button's action method and setting the delegate property of the text field to your class, you will be able to make the keyboard return on the done button. This provides a better user experience by allowing them to easily dismiss the keyboard after entering text.