cs193p – Lecture #2 – Xcode 4

Please note, this blog entry is from a previous course. You might want to check out the current one.

Lecture #2 provides a short walk through of the Xcode developing environment. The first half of the lecture discusses the various classes of the model for the playing card game. The second is a life demonstration on how to start a project in Xcodes, placing buttons and labels in Storyboard and connecting them to the view controller.

At the end of the lecture the app called Matchismo displays a playing card and allows to flip it to show both sides of the card.

Assignment #0 is to follow the steps from the demonstration, add the model classes from the first half of the lecture and finally allow to flip through all cards of a deck.

To implement this last step, add a new property to hold the model of the game – the card deck – to the view controller. Don’t forget to include the model class:

#import "PlayingCardDeck.h"
...
@property (strong, nonatomic) PlayingCardDeck *deck;

Initialize the deck using lazy instantiation:

- (PlayingCardDeck *)deck
{
    if (!_deck) {
        _deck = [[PlayingCardDeck alloc] init];
    }
    return _deck;
}

And finally draw a new card before you are showing a new one:

- (IBAction)flipCard:(UIButton *)sender
{
    if (!sender.isSelected) {
        [sender setTitle:[self.deck drawRandomCard].contents
                forState:UIControlStateSelected];
    }
    
    sender.selected = !sender.selected;
    self.flipCount++;
    
}

A drawback of this method is that after the last card of the deck has been displayed, flipping does not show a new card …

The code for this lecture including assignment #0 is available at github.

Slides have not been released yet, … Slides are available on iTunes providing a detailed walk through of the demo.

The lecture is available at iTunes and is named “2. Xcode 4 (January 10, 2013)”.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

One thought on “cs193p – Lecture #2 – Xcode 4”

  1. Hi,

    Thank you very much! This was very helpful for me.

    I started to learn iOS programming with iTunes U and was stucked in this homework where I had to create a deck initialization. I managed it to work the way that it always did a new deck (so same card could always come again and again) when I created a new object instance in flipCard but I didn’t find the way to make already created deck stay on the memory (so I don’t have to initialize a new one always).

    I had forgot that I can do that with – (PlayingCardDeck *) deck -style like you did here. This was very helpful, thanks 🙂

Leave a Reply

Your email address will not be published.