【问题标题】:How to make a keyboard return on the done button [duplicate]如何在完成按钮上使键盘返回[重复]
【发布时间】: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


【解决方案1】:

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

[myTextField resignFirstResponder];

【讨论】:

  • “让我的控件代表我的班级”是什么意思?我该怎么做呢?然后,我会将那一行代码放入我的 CellForRowAtIndexPath 方法还是我的 AccountViewCell 类中?
  • 在完成按钮的操作方法上放这行代码。
【解决方案2】:

在#.h 文件中包含UITextFeildDelegate 方法

提供[textfeild setDelegate:self];

[textField setReturnKeyType:UIReturnKeyDone];

包括

- (BOOL)textFieldShouldReturn:(UITextField *)textField
 {
[textField resignFirstResponder];
 }

【讨论】:

  • 当文本字段不再是第一响应者时,会调用textFieldDidEndEditing: 委托方法。所以在这个方法中调用resignFirstResponder 是没有意义的。当按下“return”键时,这些都不会被调用。
  • 检查我的答案。不幸的是,它显示了该方法。
【解决方案3】:

您必须在头文件中隐含UITextViewDelegate,然后将文本视图的委托设置为您的委托。通常你会在同一个类文件中做这一切,这样你就可以做到

aTextField.delegate = self;

设置好委托后,您可以使用适当的委托方法:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textView resignFirstResponder];
} 

【讨论】:

  • 这不是处理文本字段中按下的“返回”键的正确委托方法。
【解决方案4】:

在 .xib 文件中连接 UITextField 时,将目标连接到 DidEndOnExit。

【讨论】:

    猜你喜欢
    • 2014-08-22
    • 2012-09-11
    • 2012-04-22
    • 1970-01-01
    • 2011-07-09
    • 2015-04-06
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    相关资源
    最近更新 更多