ios - 如何使键盘在完成按钮上返回

标签 ios objective-c xcode uikeyboard custom-cell

<分区>

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

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

最佳答案

在完成按钮的操作方法中使用这一行代码:

[myTextField resignFirstResponder];

关于ios - 如何使键盘在完成按钮上返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15071926/

上一篇:ios - 修改按钮样式 iOS

下一篇:ios - 我如何创建一个使用 CCSpriteFrameCache 和 CCSpriteBatchNode 对象的 CSSprite 类?

相关文章:

ios - 使用 AFNetworking 的多部分 PUT 请求

ios - Xcode 6 Beta 4 存档构建错误未实现的 IR 生成功能在不同大小的类型之间进行位播

ios - 如何在顶部 UICollectionView 上制作粘性标题

iphone - 太多的 glDrawElements 导致低 fps? OpenGL ES1.1 ( iPhone )

iphone - 将 UIImage 转换为 CVPixelBufferRef

IOS模拟器9.0不运行

cocoa - Xcode - 一个 Nib ,两个类 - 第二类中的 socket 未连接?

ios - Swift 3.0 中的 Objective C 代码

objective-c - 反转 Objective-C @available?

ios - 从未打开的 ViewController 执行 Segue