Je veux limiter l'utilisateur de saisir de l'espace dans un Uitextfield. Pour cela, j'utilise ce code mais cela ne fonctionne pas ... Qu'est-ce qui ne va pas? P> P>
6 Réponses :
Le problème est est faux. L'égalité pour les chaînes est effectuée en utilisant: p> :). P> p>
Bien que cela soit vrai, ce ne va pas vous dire si votre chaîne de remplacement contient un espace, il vous indiquera simplement que la chaîne de remplacement est un seul espace.
@crackity_jones C'est vrai, mais empiriquement, ça marche assez bien.
Cela fonctionne à moins que quelqu'un ne colle pas de texte contenant des espaces
Essayez ceci (définissez ceci dans votre - (BOOL) Textfield: (Uitextfield *) Textfield DoTchangecharactersRange: (Nstring) Remplacement: (NSString *) String):
NSArray *escapeChars = [NSArray arrayWithObjects:@" ", nil]; NSArray *replaceChars = [NSArray arrayWithObjects:@"",nil]; int len = [escapeChars count]; NSMutableString *temp = [[textField text] mutableCopy]; for(int i = 0; i < len; i++) { [temp replaceOccurrencesOfString: [escapeChars objectAtIndex:i] withString:[replaceChars objectAtIndex:i] options:NSLiteralSearch range:NSMakeRange(0, [temp length])]; } [textField setText:temp]; return TRUE;
string == @" " Isn't that just going to compare the adress of each of string and @" "? Thats not the comparison you want to do.Also do you want to prevent them from entering a string that is just a space? If so then you need to change that == into a proper string comparison and you are good to go. If not and you want to prevent all spaces in an input string then you need a string matcher
Ceci recherchera pour voir si votre chaîne de remplacement contient un espace, si tel est le cas, il lance le message d'erreur, s'il ne renvoie pas oui.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSRange spaceRange = [string rangeOfString:@" "]; if (spaceRange.location != NSNotFound) { UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You have entered wrong input" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [error show]; return NO; } else { return YES; } }
La réponse actuelle est la suivante:
Définissez le contrôleur d'affichage pour vous conformer à l'UITEXTFIELDDELEGATE (devrait ressembler à ceci près du haut de votre code): P>
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField == self.yourTextField) { if ([string isEqualToString:@" "] ) { return NO; } } return YES; }
ci-dessous est ce que j'utilise pour le mot de passe et confirmez le mot de passe
in préfixheader.pch code> Ajouter ci-dessous p>
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField==passwordTF || textField==confirmPasswordTF) {
NSCharacterSet *cs = [NSCharacterSet characterSetWithCharactersInString:NONACCEPTABLE_PASSWORD_CHARACTERS];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
return YES;
}