【问题标题】:Random Card Collection Generator随机卡片收集生成器
【发布时间】:2013-08-11 02:06:17
【问题描述】:

我希望能够拥有它,以便每次单击集合中的按钮时都会生成一张新卡片。由于某种原因,这适用于 2 次点击,然后选定状态的标题变为 nil。感谢您的帮助!

 - (void) setButtonCollection:(NSMutableArray *)buttonCollection
{
_buttonCollection = buttonCollection;

for (UIButton *cardButton in self.buttonCollection){
    Card *card = [self.deck drawRandomCard];
    [cardButton setTitle:card.contents forState:UIControlStateSelected];

  }

}

- (IBAction) flipCard:(UIButton *)sender {

sender.selected = !sender.isSelected;
[self setButtonCollection: self.buttonCollection];
}

【问题讨论】:

    标签: ios objective-c uibutton setter ibaction


    【解决方案1】:

    如果我没记错的话,您正在研究 CS193P。每次单击时,您都会经历 for 循环,该循环正在为集合中的每个按钮重新绘制卡片。您可能只有 52 张牌在牌组中,单击两次后没有更多牌,因此 [self.deck drawRandomCard] 返回 nil,将 title 设置为 nil。您不必在集合的设置器中设置所有卡片,您可以在翻转卡片时将每张卡片设置在翻转卡片中。这是我的翻转卡版本。它还会检查是否没有更多卡片。让我知道这是否是您要找的。

    - (IBAction)flipCard:(UIButton *)sender {
    
        sender.selected = !sender.selected;
        if (sender.selected) {
            PlayingCard *randomCard = [self.deck drawRandomCard];
            if (!randomCard) {
                //will alert user no more cards, disable the button and set alpha to 0.3
                sender.enabled = NO;
                sender.alpha = 0.3;
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No More Cards" message:@"Game Over" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }else{
                [sender setTitle:randomCard.contents forState:UIControlStateSelected];
            }
        }
        self.flipCount++;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-04
      • 1970-01-01
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 2023-01-03
      • 2017-09-03
      相关资源
      最近更新 更多