使用 NSScanner 确定字符串是否为数字
我试图找出我的字符串是否包含任何 floatValue,如果是的话,请辞去第一响应者的职务,如果不是,则文本字段键盘应保留在屏幕上。
此代码始终隐藏键盘,即使它不是 floatValue :您知道如何使其工作吗?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSScanner *scan = [NSScanner scannerWithString:[textField text]];
if ([scan scanFloat:NULL]){
[password resignFirstResponder];
[passwordLength resignFirstResponder];
return YES;
} else {
return NO;
}
}
另外,我还没有尝试过循环,但这是一个开始,如果您有任何想法:
BOOL doesStringContain(NSString* string, NSString* string2){
for (int i=0; i<[string length]; i++) {
NSString* chr = [string substringWithRange:NSMakeRange(i, 1)];
for (int j=0; j<[string2 length]; j++){
if([chr isEqualToString:j])
return TRUE;
}
}
return FALSE;
}
非常感谢
i'm trying to find out if my string contains any floatValue, and resign the first responder if it's the case, if it's not, the textfield keyboard should stay on screen.
This code always hides the keyboard, even if it's not a floatValue : do you know how to make it work?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSScanner *scan = [NSScanner scannerWithString:[textField text]];
if ([scan scanFloat:NULL]){
[password resignFirstResponder];
[passwordLength resignFirstResponder];
return YES;
} else {
return NO;
}
}
Also, i haven't tried with loops but this is a beginning, if you have any idea :
BOOL doesStringContain(NSString* string, NSString* string2){
for (int i=0; i<[string length]; i++) {
NSString* chr = [string substringWithRange:NSMakeRange(i, 1)];
for (int j=0; j<[string2 length]; j++){
if([chr isEqualToString:j])
return TRUE;
}
}
return FALSE;
}
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSScanner 会期望您的浮点数位于字符串的开头。因此,为了解决这个问题,我们使用 setCharactersToBeStripped。
setCharactersToBeStripped 将从字符串中过滤掉所有非数字非句点字符,以便您剩下要扫描的就是您要查找的数字。
NSScanner 将匹配 int 值(不带 .),并将 int 123 等同于 123.00000。
如果您想检查是否存在非数字字符与仅数字字符,请尝试:
NSScanner will expect that your float be at the beginning of your string. So to combat that we use setCharactersToBeStripped.
setCharactersToBeStripped will filter out all non-numeric non-period characters from the string so that all you're left with to scan is the number that you're looking for.
NSScanner will match int values (without the .) as well as it will equate an int 123 to 123.00000.
If you want to check if there are non-numeric characters vs only numerics then try :
您正在实施错误的委托方法。
textFieldShouldReturn:
当用户按下返回键时调用。 (不是当控件试图“从”字段“返回”时,以防万一这就是您的想法。)您应该改为实现
textFieldShouldEndEditing:
。您可以(尝试)阻止文本字段失去第一响应者状态,并保持键盘处于打开状态。只需像您正在做的那样检查文本,然后返回 YES 或 NO(让系统更新第一响应者状态)。如果您希望在输入有效的情况下返回键关闭键盘,您应该在那里调用
endEditing:
。像这样:NO
参数意味着不要强制它,基本上允许您的textFieldShouldEndEditing:
代码首先检查文本。 (如果可以的话,您应该始终调用endEditing:
,而不是resignFirstResponder
。)请注意,由于各种原因,文本字段可能会被迫放弃第一响应者状态,因此,即使您以这种方式验证输入,在将其保存到磁盘或通过网络发送或执行任何您想要的操作之前,请准备好再次验证它。
You are implementing the wrong delegate method.
textFieldShouldReturn:
is called when the user hits the return key. (Not when control is trying to "return from" the field, in case that's what you were thinking.)You should implement
textFieldShouldEndEditing:
instead. That is where you can (try to) stop the text field from losing first responder status, and keep the keyboard up. Just check the text like you are doing, and return YES or NO (let the system update first responder status).If you want the return key to dismiss the keyboard if input is valid, you should call
endEditing:
there. Like this:The
NO
parameter means don't force it, basically allowing yourtextFieldShouldEndEditing:
code to check the text first. (You should always callendEditing:
if you can, rather thanresignFirstResponder
.)Note that, for various reasons, the text field might be forced to give up first responder status anyway, so even if you're validating input in this way, be prepared to validate it again before you save it to disk or send it over the network or whatever you want to do with it.