0

我正在调用 textfiled 确实更改了方法,但问题是我想输入 1.0,如果值不是 1.0 或 1,5 或我想要的值,那么它可能会显示错误消息,但是当我只输入 1 时,它会显示错误消息如何要解决此问题,应在输入完整的数字 1.5 或 1.0 后调用该方法,如下所示

   -(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];


   }


 }
4

5 回答 5

3
-(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];


   }


 }
于 2012-05-29T05:44:25.153 回答
1

对于这种情况,您不应使用 valueDidChange 事件,因为每次您在 textField 中进行更改时都会触发此事件,应该使用 UIControlEventEditingDidEnd 代替,当文本字段失去焦点时将触发此事件 (resignFirstResponse)

于 2012-05-29T06:04:07.137 回答
0

您可以添加观察者,当值更改UITextFieldTextDidChangeNotification时将调用您的方法 。UITextField

[[NSNotificationCenter defaultCenter] 
                addObserver:self 
                selector:@selector(textFieldTextDidChangeClinicMarkup:)
                name:UITextFieldTextDidEndEditingNotification
                object:textFieldName];
于 2012-05-29T05:43:48.477 回答
0

试试这个 UITextField 委托:

 -(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
 }
于 2012-05-29T06:07:31.910 回答
0

使用这个,它的工作

- (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];
}

希望能帮助到你。快乐编码:)

于 2012-05-29T06:08:26.347 回答