【问题标题】:Delegate method for UITextField not being called未调用 UITextField 的委托方法
【发布时间】:2015-09-17 02:46:53
【问题描述】:

我有一个带有UITextField 的tableView 单元格来输入文本。 我正在使用以下代码填充 tableView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Ingredient Cell";
    IngredientsTableViewCell *ingredientCell = [self.ingredientsTableView dequeueReusableCellWithIdentifier:cellIdentifier];

   // NSManagedObjectContext *managedObject = [self.ingredientItems objectAtIndex:indexPath.row];
    if (ingredientCell == nil)
    {
        ingredientCell = [[IngredientsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        ingredientCell.accessoryType = UITableViewCellAccessoryNone;
        [ingredientCell addSubview:ingredientCell.ingredientTextField];
        [ingredientCell.ingredientTextField addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];

    }

    //Populate the textfield in the ingredientCell
    ingredientCell.ingredientTextField.text = [self.ingredientItems objectAtIndex:indexPath.row];

    return ingredientCell;
}

以下是永远不会执行的 textField 的 @selector(editingChanged:) 方法。我做错了什么?

-(void) editingChanged:(id)sender{
    NSLog(@"hi");

    // get the text being entered
    NSString *ingredientText = ((UITextField *)sender).text;

    //get the index of the selected row
    NSInteger selectedIndex = [self.ingredientsTableView indexPathForSelectedRow].row;

    //save the text to the array
    [self.ingredientItems setObject:ingredientText atIndexedSubscript:selectedIndex];
}

【问题讨论】:

  • 顺便说一句 - editingChanged: 不是 UITextFieldDelegate 的方法。
  • 看起来那里复制和粘贴的代码太多。该单元格创建方法发生了一些奇怪的事情。您正在实例化 IngredientCell 并可能重新添加其中一个子视图?您还将子视图添加到单元格而不是其 contentView。该代码有太多错误,无法识别正在发生的事情。也许发布您的 IngredientCell 实现(这是使用情节提要中的原型单元创建的吗?)。我猜你在那个单元格中有 2 个文本字段,一个遮住了另一个。

标签: ios objective-c uitableview uitextfield


【解决方案1】:

所以你会想要使用 textFieldDelegate(或者如果你喜欢冒险,你可以使用 ReactiveCocoa)。

在顶部添加这个

<UITextFieldDelegate>

这在你的 cellForRowAtIndexPath 中

ingredientCell.ingredientTextField.delegate = self;
ingredientCell.ingredientTextField.tag = 1;

还有这个委托方法

//Use this method instead of addTarget:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 1){
        //do some stuff
    }
}

查看此堆栈溢出帖子:iOS - Adding Target/Action for UITextField Inside Custom UITableViewCell

但是您也可能遇到稍微不同的问题。这行代码似乎是多余的:

[ingredientCell addSubview:ingredientCell.ingredientTextField];

您的成分单元格似乎应该已经将成分文本字段作为子视图。这也可能会给您带来问题。您可以将其添加为附加到您的 ingredientsTableViewCell 类的 xib 中的子视图,或者只需将其添加到代码中

[self addSubview:ingredientTextField]

希望对您有所帮助。

【讨论】:

    【解决方案2】:
    if (ingredientCell == nil)
    {
        ingredientCell = [[IngredientsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        ingredientCell.accessoryType = UITableViewCellAccessoryNone;
        [ingredientCell addSubview:ingredientCell.ingredientTextField];
        [ingredientCell.ingredientTextField addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
    
    }
    

    这部分看起来有很多问题 尝试从 if 语句中取出这一行

    [ingredientCell.ingredientTextField addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
    

    这会对你有所帮助。

    【讨论】:

    • 删除需要的行如何解决问题?
    • as editingChanged: 方法仅在单元格为 nil 时作为目标添加。在每个单元格中添加它可以解决问题
    • 您只希望每个单元格添加一次目标。将其移出if 语句将在每次重用单元格时添加目标。这不是你想要的。
    【解决方案3】:

    你可以在cellForRowAtIndexPath中添加这一行,在Viewcontroller.h文件中添加UITextFieldDelegate

    ingredientCell.ingredientTextField.delegate = self;
    [ingredientCell.ingredientTextField addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
    

    这是文本字段控制器事件

    UIControlEventEditingDidBegin     = 1 << 16,     // UITextField
    UIControlEventEditingChanged      = 1 << 17,
    UIControlEventEditingDidEnd       = 1 << 18,
    UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing
    

    【讨论】:

    • 这有什么帮助?问题是关于未调用 editingChanged: 方法。这不是委托方法,因此设置文本字段的 delegate 属性不会修复它。
    • 使用这个新代码,这个代码在我的项目中完美运行
    • 有问题,委托未设置。
    • 控制事件处理程序不需要委托。
    • 是的,控制事件处理程序不需要委托。但是我的项目正在编辑更改:方法工作正常
    猜你喜欢
    • 2011-10-29
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    相关资源
    最近更新 更多