g.r*_*ion 7 iphone user-interface objective-c 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;
}
}
Run Code Online (Sandbox Code Playgroud)
但它没有用......它有什么问题?
kiy*_*shi 10
问题是
string == @" "
Run Code Online (Sandbox Code Playgroud)
是错的.字符串的平等使用:
[string isEqualToString:@" "]
Run Code Online (Sandbox Code Playgroud)
:).
cra*_*nes 10
这将搜索您的替换字符串是否包含空格,如果是,则它会抛出错误消息,如果不是,则返回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;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11190 次 |
最近记录: |