我正在输入值textfiled我希望如果它是正确的值然后它的确定其他明智的显示警报并使文本字段为空。所以用户可以输入正确的值我尝试了很多方法,但我没有得到它解决。< / p>
-(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];
}
}
答案 0 :(得分:3)
对于这个特殊问题,我会改为使用UIStepper
controll:
stepper.minimumValue = 1.0f;
stepper.maximumValue = 3.0f;
stepper.stepValue = 0.5f;
步进控件如下所示:
答案 1 :(得分:2)
@james如果您只有一个文本字段,那么您可以实现此代码:
- (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 :(得分:0)
设置代理
clinicMarkupTextField.delegate = self;
clinicMarkupTextField.returnKeyType = UIReturnKeyDone;
并尝试以下代码
-(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;
}