How to check that if text is typing and displays a warning and a blank text field in iphone

I enter the value textfiled. I want this to be the correct value, then its ok another wise to show a warning and make the text box empty. So that this user can enter the correct value, I tried many ways, but I do not solve it.

-(void)textFieldTextDidChangeClinicMarkup:(UITextField*)tf{ NSString*test=clinicMarkupTextField.text; if([test isEqualToString:@"1"]){ NSString* value=@ "1"; appDelegate.clinicalMarkup=value; } else if([test isEqualToString:@"1.5"]){ NSString* value=@ "1.5"; appDelegate.clinicalMarkup=value; } else if([test isEqualToString:@"2"]){ NSString* value=@ "2"; appDelegate.clinicalMarkup=value; } else if([test isEqualToString:@"2.5"]){ NSString* value=@ "2.5"; appDelegate.clinicalMarkup=value; } else if([test isEqualToString:@"3"]){ NSString* value=@ "3"; appDelegate.clinicalMarkup=value; } else if([test length] >=3 || [test floatValue] > 3 || [test floatValue] <1 ) { UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Clinic Markup can only be 1, 1.5, 2, 2.5 and 3 " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } } 
+4
source share
3 answers

For this specific task, I would use the UIStepper controll instead:

 stepper.minimumValue = 1.0f; stepper.maximumValue = 3.0f; stepper.stepValue = 0.5f; 

The step control is as follows:
enter image description here

+3
source

@james If you have only one text field, you can implement this code:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { NSString *strText=textField.text; if ([strText isEqualToString:@"1"]||[strText isEqualToString:@"1.5"]||[strText isEqualToString:@"2"]||[strText isEqualToString:@"2.5"]||[strText isEqualToString:@"3"]) { appDelegate.clinicalMarkup=strText; [textField resignFirstResponder]; } else { UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Clinic Markup can only be 1, 1.5, 2, 2.5 and 3 " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } return YES; } 
+2
source

Set delegate

 clinicMarkupTextField.delegate = self; clinicMarkupTextField.returnKeyType = UIReturnKeyDone; 

and try the code below

 -(BOOL)textFieldShouldReturn:(UITextField *)textField{ if(clinicMarkupTextField == textField) //only if u have more then 1 text field { NSString*test=textField.text; if([test isEqualToString:@"1"]){ NSString* value=@ "1"; appDelegate.clinicalMarkup=value; } ---SNIP--- else if([test length] >=3 || [test floatValue] > 3 || [test floatValue] <1 ) { //if all condition fails textField.text = @""; UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Clinic Markup can only be 1, 1.5, 2, 2.5 and 3 " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } } return yes; } 
0
source

Source: https://habr.com/ru/post/1415106/


All Articles