循环遍历文本字段数组并检查ALL是否为空

时间:2014-11-11 13:00:44

标签: objective-c cocoa-touch uitextfield nsarray enumeration

循环遍历UITextFields数组并检查其中是否有任何空

NSArray*textFields = ...;


textFieldHasData = YES;
[textFields enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isEqualToString:@""]) {
        textFieldHasData = NO;
        *stop = YES;
    }
}];
if (!textFieldHasData) {
    //Empty text fields found
}

我还想检查数据中 ALL textfields是否为空,而不是单独检查。

1 个答案:

答案 0 :(得分:2)

颠倒逻辑:

NSArray *textFields = ...;

textFieldHasData = NO;
[textFields enumerateObjectsUsingBlock:^(UITextField *field, NSUInteger idx, BOOL *stop) {
    if ([[field text] length] > 0) {
        textFieldHasData = YES;
        *stop = YES;
    }
}];
if (!textFieldHasData) {
    // All text fields are empty

}