If you want to make a button to reset the game data on the UI, you need to make a What? in the controller.

  ios, question

At the beginning of the game:
图片描述

After clicking the Re-Deal button:
图片描述

Click on the avatar again to find that Score has not been reset to 0
图片描述

Can you tell me how to reset Score to 0 when the game is restarted after clicking the Re-Deal button?

//  ViewController.m
 
 #import "ViewController.h"
 #import "PlayingCardDeck.h"
 #import "CardMatchingGame.h"
 
 @interface ViewController ()
 @property (nonatomic,strong) Deck *deck;
 @property (nonatomic,strong) CardMatchingGame *game;
 @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
 @property (weak, nonatomic) IBOutlet UILabel *scoreLable;
 
 @end
 
 @implementation ViewController
 
 - (IBAction)touchReDealCardButton {
 [self reDealUI];
 bracket
 
 - (void)reDealUI
 bracket
 for (UIButton *cardButton in self.cardButtons) {
 NSUInteger cardIndex = [self.cardButtons indexOfObject:cardButton];
 Card *card = [self.game cardAtIndex:cardIndex];
 [cardButton setTitle:@"" forState:UIControlStateNormal];
 [cardButton setBackgroundImage:[UIImage imageNamed:@"cardback"] forState:UIControlStateNormal];
 cardButton.enabled = YES;
 card.matched = NO;
 card.chosen = NO;
 bracket
 self.scoreLable.text = [NSString stringWithFormat:@"Score: 0"];
 bracket
 
 #pragma mark - game
 - (CardMatchingGame *)game
 bracket
 if (!  _game) {
 _game = [[CardMatchingGame alloc] initWithCardCount:self.cardButtons.count usingDeck:self.deck];
 bracket
 return _game;
 bracket
 
 #pragma mark - deck
 - (Deck *)deck
 bracket
 if (!  _deck) {
 _deck = [self createDeck];
 bracket
 return _deck;
 bracket
 
 - (Deck *)createDeck
 bracket
 return [[PlayingCardDeck alloc] init];
 bracket
 
 #pragma mark - touchCardButton
 - (IBAction)touchCardButton:(UIButton *)sender {
 NSUInteger cardIndex = [self.cardButtons indexOfObject:sender];  //Find out where this send button is in the array
 [self.game chooseCardAtIndex:cardIndex];  //Let the game choose the card at the mark!
 [self updateUI];  //Updating UI is one of the first tasks of the controller: it synchronizes the model and UI
 bracket
 
 #pragma mark - updateUI
 -(void)updateUI // traverses all the card buttons to obtain that card button, and at the same time, observe the corresponding cards in the model to ensure that the card button displays the card contents it should display.
 bracket
 For (uibutton * cardbuttons in self.cardbuttons)
 NSUInteger cardIndex = [self.cardButtons indexOfObject:cardButton];
 Card *card = [self.game cardAtIndex:cardIndex];
 [cardButton setTitle:[self titleForCard:card] forState:UIControlStateNormal];
 [cardButton setBackgroundImage:[self backgroundImageForCard:card] forState:UIControlStateNormal];
 cardButton.enabled = !  card.isMatched;
 bracket
 Text = [nsstringstringwith format: @ "score: percentage ld",self.game.score];
 bracket
 
 - (NSString *)titleForCard:(Card *)card
 bracket
 return card.isChosen ?   card.contents : @"";
 bracket
 
 - (UIImage *)backgroundImageForCard:(Card *)card
 bracket
 return [UIImage imageNamed:card.isChosen ?   @"cardfront" : @"cardback"];
 bracket
 
 @end

Yesself.scoreLable.textThe setting of the has appeared in two places altogether. At:

reDealUIIn the function:

self.scoreLable.text = [NSString stringWithFormat:@"Score: 0"];

in additionupdateUIIn the function:

Text = [nsstringstringwith format: @ "score: percentage ld",self.game.score];

So is it only necessary toreDealUIAt the same timeself.game.score = 0Is it all right?