使用分组表视图时,IOS键盘解除

时间:2013-01-22 08:36:58

标签: iphone ios uitableview keyboard dismiss

iPhone App:

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

已将接口中的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; 
}

2 个答案:

答案 0 :(得分:2)

首先尝试使用Google:search?q=textfield+resignfirstresponder+not+hiding+the+keyboard

stackoverflow上有很多这样的问题。

示例:

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

此问题有解决方法,请查看问题here

来自:https://stackoverflow.com/a/6854165/672989

答案 1 :(得分:1)

您应该将代理人分配给UITextField

您的数据源(或任何其他对象,但这个工作似乎没问题)应符合<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];
 ....

对其他UITextField执行相同操作。

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

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

修改

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

告诉编译器某个类是否为<UITextFieldDelegate>协议,只需在头文件中的类@interface声明后添加。

例如,如果您用作表的dataSource的类名为MyDataSource:

@interface MyDataSource: NSObject <UITableViewDelegate,UITextFieldDelegate>

当然,您必须实现所有必需的方法才能正确地符合某个协议。 请查看文档:{​​{3}}

编辑:至于检查电子邮件的有效性:您可以检查有效性(这实际上是另一个问题),您可以在- (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;
    }
}

关于如何查看电子邮件,请查看以下答案:UITextFieldDelegate Protocol Reference