如何设置文本字段的条件。文本?

时间:2011-07-13 06:49:06

标签: iphone xcode uiview uitextfield uipickerview

if([txtField1.text  intValue] == nil  && [txtField2.text  intValue] == nil && [txtField3.text  intValue] == nil  && [txtField4.text  intValue] == nil  && [txtField5.text  intValue] == nil && [txtField6.text  intValue] == nil && [txtField7.text  intValue] == nil && [txtField8.text  intValue] == nil )
{
    UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message: @"Please complete your test." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [info show];
     [info release];

}
else if(txtField1.text == NULL  || txtField2.text   == NULL || txtField3.text  == NULL || txtField4.text   == NULL  || txtField5.text  == NULL || txtField6.text == NULL ||txtField7.text == NULL || txtField8.text  == NULL )
{
    UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message: @"Please complete your test." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [info show];
    [info release];

}
else{
fourthScreen=[[FourthScreen alloc] initWithNibName:@"FourthScreen" bundle:nil];
    [self.navigationController pushViewController:self.fourthScreen animated:YES];
}

在上面的代码我有8个文本字段。因为我已经定义了一个条件,当所有文本字段为空时,显示警报视图信息为零。它正在发挥作用。但是,当我定义为任何人有零或空白文本字段时,也显示警报视图,但它不显示。问题是当我输入整数值“0”然后它也显示警报视图但我希望它不显示为'0'值。 picke4r视图选择的值。如何为此设置代码?

5 个答案:

答案 0 :(得分:2)

尝试:

if([txtName.text length] == 0 || [txtName.text isEqualToString:@""])
{
         // Alert here
}

将长度测试为0或将字符串与空字符串进行比较。

答案 1 :(得分:1)

如果任何文本字段为空,则不需要任何第一个条件警报应该提示。 试试这个

if([txtField1.text length] == 0  || [txtField2.text length] == 0 || [txtField3.text length]  == 0 || [txtField4.text length   == 0]  || [txtField5.text length]  == 0 || [txtField6.text length == 0 ||[txtField7.text length] == 0 || [txtField8.text length]  == 0 )
{
    UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message: @"Please complete your test." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [info show];
    [info release];

}
else{
fourthScreen=[[FourthScreen alloc] initWithNibName:@"FourthScreen" bundle:nil];
    [self.navigationController pushViewController:self.fourthScreen animated:YES];
}

答案 2 :(得分:0)

你可以这样比较0。

 if ([txtName.text isEqualToString:@"0"]) {
     // 0
 }

答案 3 :(得分:0)

检查textFields中字符串的长度是否为0:

if ([txtField1.text  length] == 0) {
    //your stuff here
}

如果文本字段中根本没有输入任何内容,则这将完全成立。您不再需要nilNULL之间的“区别”。

答案 4 :(得分:0)

if([txtName.text isEqualToString:@“”])

{

}