首先创建标签:
@property (weak, nonatomic) IBOutlet UILabel *matchInfo;
然后更改以下函数,使其返回BOOL:
-(BOOL)flipCardAtIndex:(NSUInteger)index { //Return type changed from void to BOOL
Card *card = [self cardAtIndex:index];
if (!card.isUnplayable) {
if (!card.isFaceUp) {
for (Card *otherCard in self.cards) {
if (otherCard.isFaceUp && !otherCard.isUnplayable) {
int matchScore = [card match:@[otherCard]];
if (matchScore) {
otherCard.unplayble = YES;
card.unplayble = YES;
self.score += matchScore * BONUS;
} else {
otherCard.faceUp = NO;
self.score -= MISMATCH_PENALTY;
}
break;
}
}
self.score -= FLIP_COST;
}
card.faceUp = !card.isFaceUp;
}
return matchScore;
}
最后,我们返回matchScore,这样我们的视图控制器就会知道结果,然后我们可以这样使用这些信息。
- (IBAction)flipCard:(UIButton *)sender {
if([self.game flipCardAtIndex:[self.cardButtons indexOfObject:sender]]){
self.flipsCount++;
[self updateUI];
self.matchInfo.text = @"You have a match";
}
else{
self.matchInfo.text = @"Sorry bro.";
}
}