【问题标题】:UITextfield in a Grouped UITableViewCell分组 UITableViewCell 中的 UITextfield
【发布时间】:2012-11-19 12:28:16
【问题描述】:

我正在尝试通过使用分组静态表格视图在 iPhone 应用程序上实现输入表单。

我正在使用didSelectRowAtIndexPath 懒惰地创建一个文本字段并添加到相关单元格中。

我的问题是 becomeFirstResponder 的行为不像我预期的那样。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
    if(!self.contactNameTextField)
    {
        self.contactNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(93, 14, 200, 20)];
        self.contactNameTextField.backgroundColor = [UIColor clearColor];
        self.contactNameTextField.font = [UIFont boldSystemFontOfSize:15];
        self.contactNameTextField.keyboardType = UIKeyboardTypeEmailAddress;
        [self.contactNameCell addSubview:self.contactNameTextField];
    }

    [self.contactNameTextField becomeFirstResponder];

}

if(indexPath.section == 1)
{
    if(!self.contactEmailTextField)
    {
        self.contactEmailTextField = [[UITextField alloc] initWithFrame:CGRectMake(93, 14, 200, 20)];
        self.contactEmailTextField.backgroundColor = [UIColor clearColor];
        self.contactEmailTextField.font = [UIFont boldSystemFontOfSize:15];
        self.contactEmailTextField.keyboardType = UIKeyboardTypeEmailAddress;
        [self.contactEmailCell addSubview:self.contactNameTextField];
    }

    [self.contactEmailTextField becomeFirstResponder];

}

代码按预期逐步执行。

如果我先点击第 0 部分,名称文本字段将成为第一响应者。如果我然后点击第 1 部分,电子邮件文本字段将成为第一响应者。如果我然后点击第 0 部分,即使在名称文本字段上调用 ​​becomeFirstResponder,它也不会响应。

另外,如果我先点击第 1 部分,即使在电子邮件文本字段中调用 becomeFirstResponder,它也不会响应。

请指教

【问题讨论】:

  • 在方法顶部尝试 NSLogging self.contactNameTextField 看看它是否正在保存引用。
  • 刚刚找到它,将 self.contactNameTextField 的问题剪切并粘贴两次,谢谢小伙子们

标签: ios xcode uitableview uitextfield


【解决方案1】:

在调用 becomeFirstResponder 之前尝试调用其他文本字段的 resignFirstResponder。 替换

[self.contactNameTextField becomeFirstResponder];

[self.contactEmailTextField resignFirstResponder];
[self.contactNameTextField becomeFirstResponder];

对第 1 部分执行相同操作。在调用 resignFirstResponder 之前检查 textField 是否不为零。

【讨论】:

    【解决方案2】:

    问题已解决,self.contactNameTextField 出现两次愚蠢的剪切和粘贴错误。

    【讨论】:

      猜你喜欢
      • 2011-01-30
      • 2011-09-19
      • 1970-01-01
      • 2019-11-27
      • 2010-09-25
      • 2011-06-01
      相关资源
      最近更新 更多