我正在调用textfiled做的更改方法但问题是我要输入1.0并且如果值不是1.0或1.5或我想要的值那么它可能会显示错误消息但是当我只输入1然后它显示错误消息如何修复此方法应该在输入complte number 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];
}
}
答案 0 :(得分: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];
}
}
答案 1 :(得分:1)
你不应该为这种情况使用valueDidChange事件,因为每次在textField中进行更改时都会触发此事件,而应该使用UIControlEventEditingDidEnd,当文本字段丢失焦点时会触发此事件(resignFirstResponse)
答案 2 :(得分:0)
您可以为UITextFieldTextDidChangeNotification
添加观察者,这会在UITextField
值更改时调用您的方法。
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(textFieldTextDidChangeClinicMarkup:)
name:UITextFieldTextDidEndEditingNotification
object:textFieldName];
答案 3 :(得分: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
}
答案 4 :(得分: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];
}
希望它有所帮助。快乐的编码:)