iPhone restrict user from entering space in textfield

前端 未结 6 1972
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 08:40

i want to restrict user from entering space in a UITextField. for this i m using this code

- (BOOL)textField:(UITextField *)textField shouldChangeCharacters         


        
相关标签:
6条回答
  • 2021-01-05 09:09

    The current answer is this:

    Set the View Controller to conform to the UITextFieldDelegate (should look something like this near the top of your code):

    @interface YourViewController () <UITextFieldDelegate>
    
    ...
    
    @ end
    @ implementation YourViewController
    
    ...
    
    @end
    

    Then make the textfield use the View Controller as its delegate. Do this by going to the Interface Builder, control clicking on the textfield and dragging a line to the yellow circle on the bar underneath the View Controller, and selecting "delegate" from the menu that pops up. You could alternatively do this in code by setting the delegate after making the property described in the next paragraph. Do it this way with self.yourTextField.delegate = self; in an appropriate place, possibly in viewDidLoad.

    Also set the textField up as a property on the View Controller. Do this by going to the Interface Builder, with its code open in the assistant editor, and control click and drag from the text field in the Interface Builder, to the place in the code where the properties are listed (between @interface and the first @end). Then enter a name in the pop up window. In the code below I used "yourTextField" for example. (you can skip this section, together with the outside if loop in the code below if you are sure that this is the only text field that will use the View Controller as its delegate, but it is best to plan ahead for future possibilities)

    Then you can disallow spaces from even be entered using the following delegate method:

    - (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if (textField == self.yourTextField)
        {
            if ([string isEqualToString:@" "] )
            {
                return NO;
            }
        }
        return YES;
    }
    
    0 讨论(0)
  • 2021-01-05 09:11
    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 讨论(0)
  • 2021-01-05 09:19

    This will search to see if your replacement string contains a space, if it does then it throws the error message up, if it doesn't it returns YES.

    - (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 讨论(0)
  • 2021-01-05 09:23

    The problem is

    string == @" "
    

    is wrong. Equality for strings is done using:

    [string isEqualToString:@" "]
    

    :).

    0 讨论(0)
  • 2021-01-05 09:30

    Below is what I am using for password and confirm password

    In PrefixHeader.pch file add below

    #define NONACCEPTABLE_PASSWORD_CHARACTERS @" "
    

    And in code use below.

    - (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 讨论(0)
  • 2021-01-05 09:33

    Try this (set this in your - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(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 讨论(0)
提交回复
热议问题