【问题标题】:IOS Keyboard dismissing when grouped table view is used使用分组表视图时IOS键盘关闭
【发布时间】:2013-01-05 10:35:23
【问题描述】:

iPhone 应用程序:

  • 我有一个带有 3 个文本字段的屏幕,用于输入姓名、电子邮件和位置。
  • 我使用了包含 2 个部分的分组表视图。
  • 在第一部分有三个单元格..每个单元格都有一个标签 na 文本字段..例如:名为名称的标签和一个用于输入名称的文本框..同样我也有它用于电子邮件和位置..

已将界面中的 nameTextField、emailTextField、locationTextField 声明为变量而非属性。所以请告诉我如何在这里关闭键盘

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    CommonTableViewCell* cell = [[CommonTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];//
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
    //cell.image = [UIImage imageNamed:@"resources_on.png"];
    cell.textLabel.frame = CGRectMake(TABLE_ROW_HEIGHT + 10, 5, self.view.frame.size.width , 25);

    if (indexPath.section == 0) {
        if (indexPath.row  == 0) {
            cell.textLabel.text = @"Username";

            nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
            [nameTextField setBackgroundColor:[UIColor clearColor]];
            [nameTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
            nameTextField.placeholder = @"Enter anonymous name";
            [nameTextField setKeyboardType: UIKeyboardTypeNamePhonePad];
            [cell addSubview:nameTextField];
            [nameTextField release];

        }else if (indexPath.row == 1) {

            cell.textLabel.text = @"Email"; 
            emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
            [emailTextField setBackgroundColor:[UIColor clearColor]];
            [emailTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
            emailTextField.placeholder = @"example@me.com";
            [emailTextField setKeyboardType:UIKeyboardTypeEmailAddress];
            [cell addSubview:emailTextField];
            [emailTextField release];

        }else if (indexPath.row == 2) {

            cell.textLabel.text = @"Location";
            locationTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
            [locationTextField setBackgroundColor:[UIColor clearColor]];
            [locationTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
            locationTextField.placeholder = @"Fetch";
            [cell addSubview:locationTextField];
            [locationTextField release];

        }else if (indexPath.row == 3) {

            cell.textLabel.text = @"Terms of Use";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else if (indexPath.row == 4) {

            cell.textLabel.text = @"Privacy Policy";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else {

        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {

            cell.textLabel.text = @"Terms of Use";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else if (indexPath.row == 1) {

            cell.textLabel.text = @"Privacy Policy";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else {

        }

    }


    return cell;
}

这对我不起作用..

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

【问题讨论】:

  • @user1996807:欢迎来到 SO。我绝不想按你,把我的答案标记为正确。但是,如果某个答案解决了您的问题,那么这样做是正确的:其他人现在会知道问题已经得到解答。并且写下答案的人会得到一些额外的奖励:) 如果您计划将来使用 Stack Owerflow,您还应该查看FAQ 以获取一些指导。

标签: iphone ios uitableview keyboard dismiss


【解决方案1】:

先试试谷歌:search?q=textfield+resignfirstresponder+not+hiding+the+keyboard

stackoverflow上有很多这样的问题。

示例:

此字段是否在 UIModalPresentationFormSheet 内?如果是这样,这是一个已知问题,在视图控制器被解除之前,您无法以编程方式解除键盘。

这个问题有一个解决方法,查看问题here

发件人:https://stackoverflow.com/a/6854165/672989

【讨论】:

  • 我用谷歌搜索了很多.. 不,它不在 UIModalPresentationFormSheet 中。是的,我在 stackoverflow 上看到了很多与此类似的问题,但没有任何效果。
【解决方案2】:

您应该为您的UITextFields 分配一个代表。

您的数据源(或任何其他对象,但这个似乎适合这项工作)应符合<UITextFieldDelegate> 协议。

 ...
 nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
 [nameTextField setBackgroundColor:[UIColor clearColor]];
 [nameTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
 nameTextField.placeholder = @"Enter anonymous name";
 [nameTextField setKeyboardType: UIKeyboardTypeNamePhonePad];

 [nameTextField setDelegate:self]; //add this line

 [cell addSubview:nameTextField];
 //[nameTextField resignFirstResponder]; this doesn't really make sense - creating the text
 //field should not (and does not) bring up the keyboard
 [nameTextField release];
 ....

对其他 UITextFields 执行相同操作。

然后检查您的 delegate 方法是否被调用(例如使用 NSLog 或断点):

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog (@"should return?"); 
    [textField resignFirstResponder]; 
    return YES; 
}

编辑

您只需为每个 UITextField 设置一次委托 - 通常是在创建时。

要告诉编译器某个类符合<UITextFieldDelegate> 协议,只需在头文件中的类@interface 声明之后添加即可。

例如,如果您用作表的数据源的类名为 MyDataSource:

@interface MyDataSource: NSObject <UITableViewDelegate,UITextFieldDelegate>

当然,您必须实现所有必需的方法才能正确符合某个协议。 请查看文档:UITextFieldDelegate Protocol Reference

编辑:关于检查邮件的有效性:你可以检查有效性(这实际上是另一个问题)你可以在- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

由于您的委托对象被委托给多个 UITextField,因此您必须首先检查正在编辑的文本字段是否是保存电子邮件的文本字段。而且由于您的文本字段位于表格单元格中,因此很难(并且不正确)保持对所有这些字段的引用。您可以使用标记将电子邮件文本字段与其他字段分开 - 让我们将其设为 44。稍微修改您的代码:

emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
emailTextField.tag = 44; //add this line

然后添加委托方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag == 44)
    {
        //email checking code here
    }
    else
    {
        return YES;
    }
}

至于怎么查邮件,看看这个回答:https://stackoverflow.com/a/7707946/653513

【讨论】:

  • nameTextField resignFirstResponder] 只是随机尝试。忘了删除那行。是的,非常感谢你。那么我每次都必须设置委托才能关闭键盘吗?
  • AM GETTIN THIS WARNING after setting delegate..WHERE AboutMe is a class发送“AboutMe *”到不兼容类型“id”的参数[3]跨度>
  • 是的,当然。还告诉我是否可以忽略这些警告,或者我应该对这些警告做什么
  • 忽略警告不是一个好习惯。在这种情况下,编译器告诉你你没有提供它需要的所有信息(你没有正式介绍你的类符合 UITextField 委托)。
  • 是的,非常感谢你帮了我很多。现在纠正了这些警告@rokjarc。非常感谢
猜你喜欢
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
相关资源
最近更新 更多