xcode 如何在xcode中单击按钮时调用返回bool值的函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14377042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call a function returning bool value on button's click in xcode?
提问by Ram Singh
I am new to the ios development, I am just trying to create a simple application for calculations, So for that i have used to textboxes and i want to allow only the numeric values for these. i have already set the keyboardproperty to numeric pad. Now i want to do it programmatically for that i found the following function from checks for the String contains Numeric value only
我是 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;
}
Now i want to call it on button's click, i have tried some ways but it always shows error. Please help me how can i utilize this function in button's click.
现在我想在单击按钮时调用它,我尝试了一些方法,但它总是显示错误。请帮助我如何在按钮的点击中使用此功能。
My Actual code of button is:
我的按钮实际代码是:
-(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];
}
}
Please help me.
请帮我。
I have tried the following ways for this:
为此,我尝试了以下方法:
BOOL *test= checkforNumeric(firstValue.text);
回答by Shubhank
call your Method like this
像这样调用你的方法
BOOL isValidString_withNoNumbers = [self checkforNumeric: firstValue.text];
if(isValidString_withNoNumbers)
{
//your code
}
and also you should change the method to instance method
并且您应该将方法更改为实例方法
so it should be
所以应该是
-(BOOL) checkforNumeric:(NSString*) str // - for instance method //+ for class methods
回答by P.J
Make your following line this way :-
以这种方式制作以下行:-
-(BOOL) checkforNumeric:(NSString*) str
and declare it in your .h file
并在您的 .h 文件中声明它
回答by Simone Pistecchia
if your method is in the same class, you must redifine your method, with -
如果你的方法在同一个类中,你必须重新定义你的方法,用 -
-(BOOL) checkforNumeric:(NSString*) str
and call it with:
并调用它:
[self checkforNumeric: firstValue.text];
if you define +(BOOL) checkforNumeric:(NSString*) str
in another class like MyCheckClass,
you must import MycheckClass and call it:
如果您+(BOOL) checkforNumeric:(NSString*) str
在另一个类(如 MyCheckClass )中定义,则必须导入 MycheckClass 并调用它:
BOOL isOk = [MyCheckClass checkforNumeric:firstValue.text];