如果多个 UITextField 都已填充,则启用 UIBarButtonItem
我在分组的 UITableView 中有 3 个 UITextField,并且正在尝试找出正确的逻辑,以便仅在没有 UITextField 为空时启用“保存”UIBarButtonItem。
我目前正在使用 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
UITextField 委托方法逐字符检测字段的更改,但它提供的结果不一致。
有什么想法吗?
编辑:这是我现在使用的代码。正如您所看到的,我已将文本字段放入一个数组中,以便我可以迭代它们。现在,直到我在第三个字段中输入第二个字符后,保存按钮才会启用。它还会交替启用/禁用,作为从字段中一一删除字符。
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
BOOL allValid;
if (newString.length)
{
// Cycle through array checking for completeness
for (int i = 0; i < [textFieldArray count]; i++)
{
if ([[[textFieldArray objectAtIndex:i] text] length] > 0)
{
allValid = YES;
NSLog(@"TextField #%i Validates.", i);
}
else
{
allValid = NO;
NSLog(@"TextField #%i Does Not Validate.", i);
}
}
}
else
{
NSLog(@"Invalid");
allValid = NO;
}
if (allValid)
[saveButton setEnabled:YES];
else
[saveButton setEnabled:NO];
return YES;
I have 3 UITextFields in a grouped UITableView and am trying to figure out the correct logic to only have my 'Save' UIBarButtonItem enabled when none of the UITextFields are empty.
I'm currently using the - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
UITextField delegate method to detect changes to the field character by character, but it is providing inconsistent results.
Any ideas?
Edit: Here is the code I'm now using. As you can see I've placed my text fields into an array so I can iterate through them. As it is now, the save button doesn't enable until I enter the 2nd character in the 3rd field. Also it alternates enabled/disabled as a remove characters one by one from the fields.
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
BOOL allValid;
if (newString.length)
{
// Cycle through array checking for completeness
for (int i = 0; i < [textFieldArray count]; i++)
{
if ([[[textFieldArray objectAtIndex:i] text] length] > 0)
{
allValid = YES;
NSLog(@"TextField #%i Validates.", i);
}
else
{
allValid = NO;
NSLog(@"TextField #%i Does Not Validate.", i);
}
}
}
else
{
NSLog(@"Invalid");
allValid = NO;
}
if (allValid)
[saveButton setEnabled:YES];
else
[saveButton setEnabled:NO];
return YES;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您究竟如何使用该方法?我会这样做:
但在用户按下回车键后启用按钮可能更有意义,这更容易处理。只需在文本字段上使用 didEndEditingOnExit 或 didEndEditing 事件并检查它们是否为非空。
How exactly are you using the method? Here's how I would do it:
But it might make more sense for you to enable your button after the user presses the enter key, which is easier to deal with. Just use a didEndEditingOnExit or didEndEditing event on the text fields and check there to see if they're non-empty.
好吧,这就是我最终做到的。
我创建了
- (IBAction)validateFields:(id)sender
并将其连接到 UITextField 上的 Editing Changed 出口。看起来像这样。我已经进行了相当不错的尝试,但无法让保存按钮在空文本字段的任何组合上启用,所以我想说这是正确的方法。
Ok here's how I finally did it.
I created
- (IBAction)validateFields:(id)sender
and connected it to the Editing Changed outlet on UITextField. It looks like this.I've given it a pretty decent go and couldn't get the save button to enable on any combination of empty text fields so I'm going to say this is the way to go.