如何在完成按钮上创建键盘返回

9 浏览
0 Comments

如何在完成按钮上创建键盘返回

我已经创建了一个包含文本字段的自定义单元格。当用户按下完成按钮时,我希望键盘消失(如下面的屏幕截图所示)。

自定义单元格位于"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]

我想知道是否有一种方法在按下完成键时隐藏键盘。我的应用程序截图如下所示:

keyboard does not disappear

0
0 Comments

问题的原因是没有正确实现键盘返回按钮的功能。解决方法是通过在头文件中实现UITextViewDelegate,并将文本视图的代理设置为自定义的代理。然后,在适当的代理方法中处理返回按钮被按下的事件,并调用resignFirstResponder来隐藏键盘。

0
0 Comments

[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.

0
0 Comments

问题的出现原因是没有正确设置键盘的返回按钮,并且在特定的代理方法中调用了错误的方法。解决方法是在.h文件中导入UITextFeildDelegate方法,并在设置文本输入框时将其委托给self。然后使用setReturnKeyType方法将返回按钮设置为Done。最后,实现textFieldShouldReturn方法,在其中调用resignFirstResponder方法以隐藏键盘。

0