-1

我希望能够拥有它,以便每次单击集合中的按钮时都会生成一张新卡。由于某种原因,这适用于 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];
}
4

1 回答 1

2

如果我没记错的话,你正在研究 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++;
}
于 2013-08-11T02:29:27.380 回答