-1

在查看此链接中的答案后,我创建了文本字段

如何检查用户是否在数组中所有创建的文本字段中输入了一些文本?如果任何文本字段为空,则应返回 false,否则返回 true。

编辑:另请参阅此链接。我得到数组中的文本字段。参考答案代码末尾的 nslog 中的答案。

 - (IBAction)save:(id)sender {

    mutableTextArray = [[NSMutableArray alloc] init];
    for(field in self.scroll.subviews)
    {
        if([field isKindOfClass:[UITextField class]])
        {
            if([[field text] length] > 0)
            {
                [mutableTextArray addObject:field.text];
            }

        }
    }

    NSLog(@"Save button 2 : %@", mutableTextArray);

    [self fetchStrings:mutableTextArray];

}

- (BOOL) isAllTextFieldsValid {
    for (UITextField *fields in mutableTextArray) {
        // Trim any spaces entered by user.
        NSString *txt = [fields.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if([txt length]==0) {
            //If any of the contained txt fields will have
            //blank text this will return NO.
            return NO;
        }
    }
    //Else if all txt fields have some text entered it them, return YES
    return YES;
}

- (void) fetchStrings:(NSMutableArray *)textArray
{

    NSLog(@"Array string = %@", textArray);
    if(![self isAllTextFieldsValid])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot proceed" message:@"Please answer all questions" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
    }
    else
    {
4

3 回答 3

0

如果您有一个 uitextfields 数组,只需使用 for 循环并检查数组中的所有对象是否有此语句:

if ([self.textField.text length] > 0 || [self.textField.text isEqualToString:@""] == FALSE){
//textfield has text
}
于 2013-11-14T08:58:45.343 回答
0

我认为您需要做的是编写一个带有for-loop 的简单函数来遍历这样的文本字段数组,

- (BOOL) isAllTextFieldsValid {
    for (UITextField *field in self.myTextFiledArray) {
        // Trim any spaces entered by user.
        NSString *txt = [field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if([txt length]==0) {
            //If any of the contained txt fields will have
            //blank text this will return NO.
            return NO;
        }
    }
    //Else if all txt fields have some text entered it them, return YES
    return YES;
}

将此函数称为,

if([object isAllTextFieldsValid]) {
    //All txt fields have text entered
}
else {
    //Show alert to user for filling all text fields,etc.
}

希望有帮助!

于 2013-11-14T09:13:31.323 回答
0

我刚刚用容量初始化数组并将其与全局整数“i”进行比较,其中“i”具有动态创建的文本字段计数。如果我不提它就行不通initWithCapacity:0。疯狂的。

mutableTextArray = [[NSMutableArray alloc] initWithCapacity:0];
    for(field in self.scroll.subviews)
    {
        if([field isKindOfClass:[UITextField class]])
        {
            if([[field text] length] > 0)
            {
                [mutableTextArray addObject:field.text];
            }

        }
    }

    NSLog(@"Save button 2 : %@", mutableTextArray);
    if([mutableTextArray count] != i)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot proceed" message:@"Please answer all questions" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return;
    }

    [self fetchStrings:mutableTextArray];
于 2013-11-18T05:15:18.903 回答