0

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 keyboard property 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

+(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);
Community
  • 1
  • 1
Ram Singh
  • 5,979
  • 29
  • 93
  • 156
  • Care to share the error you're having and which code is producing the error? – Carl Veazey Jan 17 '13 at 10:36
  • Please check [this](http://stackoverflow.com/questions/1053592/objective-c-class-vs-instance-methods) question to get familiar with class/instance methods, you might want to share what you've actually tried as well so that anyone can see what goes wrong. – A-Live Jan 17 '13 at 10:41
  • 1
    downvoted because there is no error message, also not added the method call in your code. It's too localized question. It can be easily fixed by a single googling. Also there are a lot of duplicates in SO (similar one: http://stackoverflow.com/questions/9731126/how-to-call-method-from-one-class-in-another-ios). – Midhun MP Jan 17 '13 at 11:13
  • @MidhunMP sir i have already mentioned in the question that i am new to the iphone development so .. and i have pasted only the working code.. i have tried few ways that we used in ASP.NET but that were not working so i googled when i didnt get that understood to me so i paste the code and question here... hope you will remind your old days when you were a beginner..... – Ram Singh Jan 17 '13 at 11:49

3 Answers3

4

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
Shubhank
  • 21,443
  • 7
  • 62
  • 83
  • Its throwing exception :: warning: (Messages without a matching method signature will be assumed to return id and accept....''as argument. – Ram Singh Jan 17 '13 at 10:42
  • @raman Call it with `BOOL flag = [Demo1ViewController checkforNumeric:textToCheck];` I don't understand why they are trying to force you using instance methods. – A-Live Jan 17 '13 at 10:43
  • I have tried this but still throwing the same exception – Ram Singh Jan 17 '13 at 10:44
  • @honey-bunny i have missed the declaration for that method because of that i was getting those problems.. – Ram Singh Jan 17 '13 at 13:02
0

Make your following line this way :-

-(BOOL) checkforNumeric:(NSString*) str 

and declare it in your .h file

P.J
  • 6,725
  • 9
  • 41
  • 73
0

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 isOk = [MyCheckClass checkforNumeric:firstValue.text];
Simone Pistecchia
  • 2,089
  • 2
  • 13
  • 21