ios - 如何判断是否未选择数组中的所有按钮

标签 ios objective-c arrays uibutton

我有一个按钮数组:

pokemon_cards = [[NSMutableArray alloc] init];
[pokemon_cards addObject:self.cardButton1];
[pokemon_cards addObject:self.cardButton2];
[pokemon_cards addObject:self.cardButton3];

稍后在某种方法中,我想做一个 BOOL 检查,看看是否所有的人都没有同时被选中。换句话说,如果没有选择所有这些通知用户,否则继续,即使只选择了其中一个。

这是我所做的,但它不起作用,如果不将循环中的按钮添加到临时数组,我无法弄清楚如何做到这一点:

-(BOOL)userCanProceedToTurn {
   for (UIButton *button in pokemon_cards) {
      if (![button isSelected]) {
        // This only works for OR condition I want it to work for &&
         return NO;
      } else {
         return YES;
      }
   }
}

这就是我想要它做的事情,但上面的功能不适用于 &&:

if (![self.cardButton1 isSelected] && ![self.cardButton2 isSelected] && ![self.cardButton3 isSelected]) {
   //notify users they must selected at least one card
} else {

}

但我不知道哪些卡片将被添加到数组中,这取决于用户,所以我不知道如何在 for 循环中检查它

编辑

我已经按照下面的建议实现了代码。如前所述,这不是我关心的 && 检查。

例如,我需要确保所有卡片当前不处于“未选择”状态。但是如果这 3 张牌中有一张是那么他们就可以继续,即使另外两张不是。但是通过下面的检查,它不会继续,因为 else 语句也在循环中,所以每次循环运行时,未选择的按钮导致它无法继续,因为循环运行了 3 次。

这是我完整的 bool 方法,除了第一个按钮,其他一切正常:

-(BOOL)userCanProceedToTurn {

    if (self.energyAmount == 0) {
     UIAlertView *view .. tell users they need energy before proceeding
     return NO;
    }
    if (self.usernameLabel.text.length == 0) {
      //Tell user they are not signed in
      return NO;
    }
    NSLog(@"button is %lu", (unsigned long)pokemon_cards.count);
    for (UIButton *button in pokemon_cards) {
      if ([button isSelected]) {
        NSLog(@"button.tag == %lu",button.tag);
        return YES;
      } else {
           UIAlertView *view .. tell users they need to select at least one card
         //this gets called because the loop is ran as many times there are buttons so inevitably i'll get an error. Which is why this works for the first button only, because it stops looping after it found that one since it was the first once selected
         return NO;
      } 
    }

    return YES;
}

最佳答案

您真的需要知道每个按钮的状态吗? 为什么不采取相反的方法:

- (BOOL)userCanProceedToTurn {
    for (UIButton *button in pokemon_cards) {
        if ([button isSelected]) {
            return YES;
        }
    }
    return NO;
}
编辑

根据经验,返回 BOOL 值的方法应以设置为 YESNO 的标志开始,并且仅将其反转标志,永远不要将其设置回其原始值。所以基本上,从 BOOL result = YES 开始,只将其翻转为 NO,永远不要将其翻转回 YES。这将具有防止不良事件发生的伪安全性。

这是用这个概念重写的方法:

- (BOOL)userCanProceedToTurn {
    BOOL isEverythingOK = NO;
    NSString *message = nil;

    if (self.energyAmount != 0) {
        isEverythingOK = YES;
    } else {
        message = @"You need energy before proceeding.";
    }

    if (self.usernameLabel.text.length != 0) {
        isEverythingOK = YES;
    } else {
        message = @"You are not signed in.";
    }

    for (UIButton *button in pokemon_cards) {
        if ([button isSelected]) {
            isEverythingOK = YES;
        } else {
            message = @"You need to select at least one card"
        }
    }

    if (!isEverythingOK) {
        UIAlertView *alert = [[UIAlertView alloc] initWith use the message here]
    }
    return isEverythingOK
}

关于ios - 如何判断是否未选择数组中的所有按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32469324/

相关文章:

ios - 将iOS项目转换为OSX

ios - Swift 2.2 泛型 : "Cannot convert return expression of type ItemPageControllerFactory to return type T"

iOS制作全屏截图问题

ios - 如何在识别出 faceId 后导航到另一个页面?

ios - 核心数据获取多对一关系对象

objective-c - 使用 CoreAnimation 制作帧动画

c++ - C++中对数组的误解

python - numpy 数组中两组值之间的距离

objective-c - 如何将 iOS 中的谷歌地图导航集成到我们的应用程序中?

c++ - 定义不带终止符的 C++ 字符数组