![](/img/trans.png)
[英]UITextField delegate method not being called (shouldChangeCharactersInRange)
[英]Delegate method for UITextField not being called
我有一个带有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];
}
因此,您需要使用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-在自定义UITableViewCell内为UITextField添加目标/操作
但是您可能也会遇到一个稍微不同的问题。 这行代码似乎是多余的:
[ingredientCell addSubview:ingredientCell.ingredientTextField];
您是成分单元格似乎应该已经具有IngredientTextText作为子视图。 这也可能导致您遇到问题。 您可以将其作为添加到IngredientTableTableViewCell类的xib的子视图添加,也可以简单地将其添加为代码
[self addSubview:ingredientTextField]
希望能有所帮助。
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];
这将为您提供帮助。
您可以在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.