Please note, this blog entry is from a previous course. You might want to check out the current one.
Do something sensible when no more cards are in the deck and the user requests more.
There a various “sensible” solutions, lets just disable the button and gray it out, when there are no cards left in the deck:
- (IBAction)addCardsButtonPressed:(UIButton *)sender { ... if (self.game.deckIsEmpty) { sender.enabled = NO; sender.alpha = 0.5; } }
To be able to enable the button again, create an outlet from storyboard:
@property (weak, nonatomic) IBOutlet UIButton *addCardsButton;
… and use it when the deal button has been pressed. At the same time let the collection view know that the cards have changed:
- (IBAction)dealButtonPressed:(UIButton *)sender { ... if (!self.game.deckIsEmpty) { self.addCardsButton.enabled = YES; self.addCardsButton.alpha = 1.0; } [self.cardCollectionView reloadData]; ... }
At the property to the game model (which we just used above):
@property (nonatomic) BOOL deckIsEmpty;
… and implement its getter to reflect the state of the deck:
- (BOOL)deckIsEmpty { if (self.deck.numberOfCardsInDeck) return NO; return YES; }
… which needs a slight adjustment of the deck model:
// Deck.h @property (nonatomic) int numberOfCardsInDeck; // Deck.m - (int)numberOfCardsInDeck { return [self.cards count]; }
The complete code is available on github.
![Share on Facebook Facebook](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/16x16/facebook.png)
![Share on Twitter twitter](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/16x16/twitter.png)
![Share on Reddit reddit](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/16x16/reddit.png)
![Pin it with Pinterest pinterest](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/16x16/pinterest.png)
![Share on Linkedin linkedin](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/16x16/linkedin.png)
![Share on tumblr tumblr](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/16x16/tumblr.png)
![Share by email mail](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/16x16/mail.png)
![Share on Facebook Facebook](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/facebook.png)
![Share on Twitter twitter](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/twitter.png)
![Share on Reddit reddit](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/reddit.png)
![Pin it with Pinterest pinterest](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/pinterest.png)
![Share on Linkedin linkedin](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/linkedin.png)
![Share on tumblr tumblr](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/tumblr.png)
![Share by email mail](https://cs193p.m2m.at/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/mail.png)