我有一个包含3个静态单元格的tableViewController。 每个单元格都有一个UITextField。
我正在尝试计算每个textField中的文本长度,以启用或禁用按钮。
为此,我使用textField委托方法
-textField:shouldChangeCharactersInRange:replacementString:
但我无法获得正确的值,例如在密码字段中,我想在长度超过6个字母时启用按钮,在长度小于6个字母时禁用。
目前我无法获得正确的长度,例如禁用密码字段中的按钮我需要达到3个字符,否则它不会被禁用。这对我来说是错误的,因为当字符小于6时,按钮必须禁用。
这是我的代码你可以解释一下我错了吗?
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([self.passwordTextField.text length] >6 && [self.nameTextField.text length] >2 && [self.emailTextField.text length] >2) [self.stepCompleted setEnabled:YES];
else if ([self.passwordTextField.text length] <6 || [self.nameTextField.text length] <2 || [self.emailTextField.text length] <2) [self.stepCompleted setEnabled:NO];
return YES;
}
答案 0 :(得分:1)
首先,您需要检查textfield == self.passwordTextField
或任何其他文本字段是否包含,然后您应用您的条件。
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (string==nil || [string isEqualToString:@""] || [string isKindOfClass:[NSNull class]] || string.length < 1)
{
if([self.nameTextField.text length] < 2 || [self.emailTextField.text length] < 2 || [self.passwordTextField.text length] < 6)
{
[self.stepCompleted setEnabled:NO];
}
}
else if(textField == self.passwordTextField && [self.passwordTextField.text length] > 6)
{
if([self.nameTextField.text length] > 2 && [self.emailTextField.text length] > 2)
{
[self.stepCompleted setEnabled:YES];
}
else
{
[self.stepCompleted setEnabled:NO];
}
}
else if(textField == self.nameTextField && [self.nameTextField.text length] > 2)
{
if([self.passwordTextField.text length] > 6 && [self.emailTextField.text length] > 2)
{
[self.stepCompleted setEnabled:YES];
}
else
{
[self.stepCompleted setEnabled:NO];
}
}
else if(textField == self.emailTextField && [self.emailTextField.text length] > 2)
{
if([self.nameTextField.text length] > 2 && [self.passwordTextField.text length] > 6)
{
[self.stepCompleted setEnabled:YES];
}
else
{
[self.stepCompleted setEnabled:NO];
}
}
return YES;
}
答案 1 :(得分:0)
您好找到这个问题的解决方案..
如果我们需要在多个UITextField
内建立最少数量的字符,我们可以为我们感兴趣的文本字段建立最小限制,并控制方法中剩余文本字段的行为......
Es ..我们有3个UITextField
名称 - 电子邮件 - 密码
在此示例中,如果文本字段具有最小字符
,我们希望启用或禁用按钮-(BOOL)textFieldShouldClear:(UITextField *)textField {
[button setEnabled:NO];
return YES;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// Otteniamo il numero di caratteri corretti
NSString *characters = [textField.text stringByReplacingCharactersInRange:range withString:string];
int minimumCharacter = 2;
int minimumCharacterForPassoword = 6;
// Identifichiamo la textField Generica con la textField che andremo ad utilizzare inoltre aggiungiamo anche il numero di caratteri MINIMI che vogliamo utilizzare per attivare il bottone Continua e quindi consentire all'utente di proseguire con la registrazione, altrimenti, se non tutti i requisiti delle textField vengono soddisfatti disabilitiamo il pulsante Continua fino a quando tutti campi non saranno completati
// NAME
if (textField == self.firstNameTextField) {
if (characters.length >= minimumCharacter) {
if ((self.passwordTextField.text.length +1) > minimumCharacterForPassoword && (self.emailTextField.text.length +1) > minimumCharacter) [_stepCompletedButton setEnabled:YES];
else [_stepCompletedButton setEnabled:NO];
}
else [_stepCompletedButton setEnabled:NO];
}
// EMAIL
if (textField == self.emailTextField) {
if (characters.length >= minimumCharacter) {
if ((self.passwordTextField.text.length +1) > minimumCharacterForPassoword && (self.firstNameTextField.text.length +1) > minimumCharacter) [_stepCompletedButton setEnabled:YES];
else [_stepCompletedButton setEnabled:NO];
}
else [_stepCompletedButton setEnabled:NO];
}
// PASSWORD
if (textField == self.passwordTextField) {
if (characters.length >= minimumCharacterForPassoword) {
if ((self.firstNameTextField.text.length +1) > minimumCharacter && (self.emailTextField.text.length +1) > minimumCharacter) [_stepCompletedButton setEnabled:YES];
else [_stepCompletedButton setEnabled:NO];
}
else [_stepCompletedButton setEnabled:NO];
}
return YES;
}