我在自定义单元格中有一个UITextField。我在viewController中有一个按钮,当单元格中的UITextField中有文本时,我想变成红色。当UITextField在viewController中有,但是当UITextView在单元格内时,我创建了这样的按钮。我该怎么做呢。
我将如何做到这一点是UITextField在viewController中 -
- (void)textFieldDidChange:(id)sender {
if (self.schoolText.text.length > 0) {
self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
}else{
self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
}
}
答案 0 :(得分:0)
您可以使用下面的UITextFieldDelegate
方法检查长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
需要注意的一点是,在文本字段实际更新其文本之前调用上述方法,因此为了克服这一点,您可以使用以下行。
[textField.text stringByReplacingCharactersInRange:range withString:string].length
答案 1 :(得分:0)
为了清楚起见,我假设您创建了一个自定义UITableViewCell
,其UITextField
的出口名为theTextField
。并且您已将故事板中的UITextField
连接到自定义单元格。
现在当您将数据填充到单元格中时
-tableView: cellForRowAtIndexPath:
您必须执行以下任一操作:
如果要在文本更改时更改按钮的颜色,请使用:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextFieldTextDidChangeNotification object:cell.theTextField]
虽然textChanged:
是这样的方法:
- (void) textChanged:(NSNotification *) notification
或者如果代表足够,只需使用cell.theTextField.delegate = self;
就是这样