7
votes

L'iPhone restreint l'utilisateur d'entrer dans l'espace dans Textfield

Je veux limiter l'utilisateur de saisir de l'espace dans un Uitextfield. Pour cela, j'utilise ce code xxx

mais cela ne fonctionne pas ... Qu'est-ce qui ne va pas?


0 commentaires

6 Réponses :


10
votes

Le problème est xxx

est faux. L'égalité pour les chaînes est effectuée en utilisant: xxx

:).


3 commentaires

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



2
votes

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;


0 commentaires

1
votes
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

0 commentaires

10
votes

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;
    }
}


0 commentaires

3
votes

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;
}


0 commentaires

0
votes

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;
}


0 commentaires