james
james

Reputation: 646

how to call the UITextFiled Did Change Method in iPhone app

I am calling the textfiled did change method but probelm is that i want to enter 1.0 and if value is not 1.0 or 1,5 or my desired values then it may show error message but when i just enter 1 then it shows the error message how to fix this that method should be called after entering complte number 1.5 or 1.0 like this

   -(void)textFieldTextDidChangeClinicMarkup:(UITextField*)tf{

   NSString*test=clinicMarkupTextField.text;


   if([test isEqualToString:@"1.0"]){
    NSString*value=@"1.0";

    appDelegate.clinicalMarkup=value;


   }

   else if([test isEqualToString:@"1.5"]){

    NSString*value=@"1.5";

    appDelegate.clinicalMarkup=value;


  }


  else if([test isEqualToString:@"2.0"]){

    NSString*value=@"2.0";

    appDelegate.clinicalMarkup=value;


 }


 else if([test isEqualToString:@"2.5"]){

    NSString*value=@"2.5";

    appDelegate.clinicalMarkup=value;


}


  else if([test isEqualToString:@"3.0"]){

    NSString*value=@"3.0";

    appDelegate.clinicalMarkup=value;


  }

  else{

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Value Must be as prescribed " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];


   }


 }

Upvotes: 0

Views: 879

Answers (5)

Anshuk Garg
Anshuk Garg

Reputation: 1540

use this, its working

- (void) textFieldDidEndEditing:(UITextField *)textField {
            if([self validateValue:textField.text] != 1)
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Value!!" message:@"Please press OK and enter again" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                [alert show];
                [alert release];
            }
}
- (BOOL) validateValue: (NSString *) candidate 
{

    NSString *valueRegex = @"[1-3]+.[0,5]{1}"; //change values here according to ur need
    NSPredicate *valueTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", valueRegex];
    return [valueTest evaluateWithObject:candidate];
}

hope it helps. happy coding :)

Upvotes: 0

Musthafa
Musthafa

Reputation: 862

You can add observer for the UITextFieldTextDidChangeNotification, that will call your method when UITextField value Change.

[[NSNotificationCenter defaultCenter] 
                addObserver:self 
                selector:@selector(textFieldTextDidChangeClinicMarkup:)
                name:UITextFieldTextDidEndEditingNotification
                object:textFieldName];

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38249

Try this UITextField delegate:

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
 {
    int currentTxtLen = [textField.text length] ;
    if (currentTxtLen == 3)
    {
        if([textField.text isEqualToString:@"1.0"] || [textField.text isEqualToString:@"1.5"]  || [textField.text isEqualToString:@"2.0"] || [textField.text isEqualToString:@"2.5"] || [textField.text isEqualToString:@"3.0"]) {
        return YES; // Requied Output
    }
    return NO;// Not required Output
 }

Upvotes: 0

Saad
Saad

Reputation: 8947

-(void)textFieldTextDidChangeClinicMarkup:(UITextField*)tf{

   NSString*test=clinicMarkupTextField.text;


   if([test isEqualToString:@"1.0"]){
    NSString*value=@"1.0";

    appDelegate.clinicalMarkup=value;


   }

   else if([test isEqualToString:@"1.5"]){

    NSString*value=@"1.5";

    appDelegate.clinicalMarkup=value;


  }


  else if([test isEqualToString:@"2.0"]){

    NSString*value=@"2.0";

    appDelegate.clinicalMarkup=value;


 }


 else if([test isEqualToString:@"2.5"]){

    NSString*value=@"2.5";

    appDelegate.clinicalMarkup=value;


}


  else if([test isEqualToString:@"3.0"]){

    NSString*value=@"3.0";

    appDelegate.clinicalMarkup=value;


  }

  else if([test length] >=3 || [test floatValue] > 3 || [test floatValue] <1 )  {

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Value Must be as prescribed " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];


   }


 }

Upvotes: 2

Son Nguyen
Son Nguyen

Reputation: 3491

You should not use valueDidChange event for this case because this event will be fired every time you make a change in the textField, should use UIControlEventEditingDidEnd instead, this event will be fired when the textfield is lost focus (resignFirstResponse)

Upvotes: 1

Related Questions