iPhone限制用户在文本字段中输入空间

问题描述:

我想限制用户在UITextField中输入空格。为此我使用此代码

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

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ( string == @" " ){
        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;
    }
}

但它不起作用....什么是错了吗?

but it is not working .... what is wrong in it ?

问题是

string == @" "

错误。字符串的平等使用:

is wrong. Equality for strings is done using:

[string isEqualToString:@" "]

:)。