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

Posted

技术标签:

【中文标题】如何为文本字段设置条件。文本?【英文标题】:how set condition for text field. text? 【发布时间】:2011-07-13 06:49:06 【问题描述】:
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 个文本字段。为此,我定义了一个条件,即当所有文本字段为 nil 时,显示警报视图消息。它正在工作。但是当我定义为当任何人有零或空白文本字段时,也会显示警报视图但它不显示。问题是当我输入整数值“0”时,它也会显示警报视图,但我希望它不显示“0”值。 picke4r 视图选择的值。如何设置代码?

【问题讨论】:

【参考方案1】:

试试:

if([txtName.text length] == 0 || [txtName.text isEqualToString:@""])

         // Alert here

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

【讨论】:

第二个比较是多余的。 NSString 只能等于 "",如果它的长度为 0。【参考方案2】:

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

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

【讨论】:

【参考方案3】:

你可以这样比较 0。

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

【讨论】:

【参考方案4】:

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

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

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

【讨论】:

【参考方案5】:

if ([txtName.text isEqualToString:@""])

【讨论】:

以上是关于如何为文本字段设置条件。文本?的主要内容,如果未能解决你的问题,请参考以下文章

如何为 UITextField 中的占位符文本设置可访问性特征?

如何为文本小部件设置字符的最大宽度?

如何为FLASH文本设置字体、大小、颜色、段落等属性?还可以为FLASH文本设置哪

如何为 UITableView 标题设置颜色文本颜色

如何为 QPlainTextEdit 设置文本?

如何为状态 UIControlStateHighlighted 设置按钮标签文本颜色