本文介绍了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:@" "]

:)。

这篇关于iPhone限制用户在文本字段中输入空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 15:16