随机卡收集生成器

时间:2013-08-11 02:06:18

标签: ios objective-c uibutton setter ibaction

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

 - (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];
}

1 个答案:

答案 0 :(得分:2)

如果我没弄错的话你正在研究CS193P。每次点击,您都会通过for循环,它会为集合中的每个按钮重新绘制卡片。您可能在牌组中只有52张牌,两次点击之后没有更多牌,因此[self.deck drawRandomCard]返回nil,将title设置为nil。您不必在集合的setter中设置所有卡片,只需在翻转时将每张卡片设置在flipCard中。这是我的flipCard版本。它还会检查是否没有卡。如果您正在寻找,请告诉我。

- (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++;
}