我是 ios 开发的新手,我只是想创建一个简单的计算应用程序,所以我已经习惯了文本框,我只想允许这些数字值。我已经将键盘属性设置为数字键盘。现在我想以编程方式执行此操作,因为我从检查字符串中发现以下函数仅包含数字值
+(BOOL) checkforNumeric:(NSString*) str
{
NSString *strMatchstring=@"\\b([0-9%_.+\\-]+)\\b";
NSPredicate *textpredicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@", strMatchstring];
if(![textpredicate evaluateWithObject:str])
{
//////NSLog(@"Invalid email address found");
UIAlertView *objAlert = [[UIAlertView alloc] initWithTitle:APP_NAME message:@"please enter valid text." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Close",nil];
[objAlert show];
[objAlert release];
return FALSE;
}
return TRUE;
}
现在我想在单击按钮时调用它,我尝试了一些方法,但它总是显示错误。请帮助我如何在按钮的点击中使用此功能。
我的实际按钮代码是:
-(IBAction)button{
if (firstValue.text.length>0 && secondvalue.text.length>0) {
label.text= [ [NSString alloc] initWithFormat:@"Result is: %.2f", ([firstValue.text floatValue]) * ([secondvalue.text floatValue])];
}
else if (firstValue.text.length==0 && secondvalue.text.length==0){
//label.text= @"please enter the values.";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please Enetr First Value and second value." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}else if (firstValue.text.length==0) {
//label.text= @"please enter the first values.";
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please Enter First Value" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}else if (secondvalue.text.length==0) {
//label.text= @"please enter the second values.";
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please Eneter Second Value" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
请帮我。
为此,我尝试了以下方法:
BOOL *test= checkforNumeric(firstValue.text);