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

标签 iphone objective-c user-interface uitextfield

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

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

但它不工作....它有什么问题?

最佳答案

问题是

string == @" "

错了。字符串的相等性使用:

[string isEqualToString:@" "]

:).

关于iPhone 限制用户在文本字段中输入空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1304321/

相关文章:

c# - 如何制作 "greenscreen"网络应用程序?

iphone - 使用 Three20 和 iOS6 的架构 armv7s 的 undefined symbol

iphone - UIScrollview 上的“静态”UIButton?

iphone - 保持背景的滑动菜单(Xcode 4.3)

objective-c - 为什么 UIScrollview 在 Interface Builder 中没有辅助功能区域?

objective-c - 如何在 Objective-C 中设置 CAShapeLayer 透明?

windows - 如何防止 Windows 中的焦点窃取?

iphone - NSURLConnection缓存和接收数据

iphone - 如何将表情符号和文本一起保存到 sqlite 数据库中

java - 关于java中的build gui和widgets