Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't update the UI
    primarykey
    data
    text
    <p>In my matching cards game, i'v created a <code>IBAction</code> method for a newGame.</p> <p>Now, I have a method called <code>updateUI</code> to change the state of the <code>UI</code> according to the actions.</p> <p>In my <code>updateUI</code> method I have a im calling alpha to fade away the matching cards, But when i'm calling the <code>newGame</code> method, The cards still fade away from the time i called the alpha...</p> <p>This is my <code>ViewController</code>, Please tell me what i'm doing wrong..thanks! :</p> <pre><code>#import "CardGameViewController.h" #import "PlayingCardsDeck.h" #import "CardMatchingGame.h" @interface CardGameViewController () @property (weak, nonatomic) IBOutlet UILabel *flipsLabel; @property (weak, nonatomic) IBOutlet UILabel *notificationLabel; @property (weak, nonatomic) IBOutlet UILabel *scoreCounter; @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons; @property (strong, nonatomic) CardMatchingGame *game; @property (nonatomic) int flipsCount; @end @implementation CardGameViewController //creating the getter method that creates a new card game. -(CardMatchingGame *) game { if (!_game) _game = [[CardMatchingGame alloc] initWithCardCount:self.cardButtons.count usingDeck:[[PlayingCardsDeck alloc] init]]; return _game; } //creating a setter for the IBOutletCollection cardButtons -(void) setCardButtons:(NSArray *)cardButtons { _cardButtons = cardButtons; [self updateUI]; } //creating the setter for the flipCount property. Whick is setting the flipsLabel to the right text and adding the number of counts. -(void) setFlipsCount:(int)flipsCount { _flipsCount = flipsCount; self.flipsLabel.text = [NSString stringWithFormat:@"Flips: %d", self.flipsCount]; } -(void) updateUI { for (UIButton *cardButton in self.cardButtons) { Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]]; [cardButton setTitle:card.contents forState:UIControlStateSelected]; [cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled]; cardButton.selected = card.isFaceUp; cardButton.enabled = !card.unplayble; if (card.unplayble) { cardButton.alpha = 0.1; } self.scoreCounter.text = [NSString stringWithFormat:@"Score: %d", self.game.score]; if (self.game.notification) { self.notificationLabel.text = self.game.notification; } } } //Here I created a method to flipCards when the card is selected, and give the user a random card from the deck each time he flips the card. After each flip i'm incrementing the flipCount setter by one. - (IBAction)flipCard:(UIButton *)sender { [self.game flipCardAtIndex:[self.cardButtons indexOfObject:sender]]; self.flipsCount++; [self updateUI]; } - (IBAction)newGame:(UIButton *)sender { [self.game cleanHistory]; self.flipsCount = 0; self.game = nil; [self updateUI]; } @end </code></pre> <p>This is the cleanHistory method:</p> <pre><code>-(void)cleanHistory { for (Card *card in self.cards) { card.unplayble = NO; card.faceUp = NO; } self.notification = nil; self.score = 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload