Improve company productivity with a Business Account.Sign Up


"Restart" the game or app in xcode / iOS

Posted on 2013-02-06
Medium Priority
Last Modified: 2013-02-07
I am learning about MVC. I have a SUPER simple app I made, plus I did the card matching game from the Stanford iOS lectures.

My question is when I start tapping increment and it increments up to like 10, I want to make a button that says "reset" and when it's tapped, it reset the counter back to 0.

how can I do this?

my basic app for learning about MVC
also, I want to add the same functionality to the card game from Stanford I watched the lectures on:

What lines of code do I need to reset the game to start over?
Question by:Mark_Co
  • 2
  • 2
LVL 14

Assisted Solution

binaryevo earned 2000 total points
ID: 38865251
You can simply set whatever variable your incrementing back to 0 or you can set the text of the label to @"0".  See below:

 int myTestInteger = 10;
 myTestInteger = 0;
 self.mainToolbarTitle.text = [NSString stringWithFormat:@"%d", myTestInteger];

Open in new window


self.mainToolbarTitle.text = @"0";

Open in new window

I think that should answer your question.  Let me know if you need more info.

Hope this helps...

Author Comment

ID: 38866004
Perfect! But, any clue how I can do this for my stanford lecture app?

//  CardGameViewController.m
//  Matchismo
//  Created by  on 2/2/13.
//  Copyright (c) 2013 . All rights reserved.

#import "CardGameViewController.h"
#import "PlayingCardDeck.h"
#import "CardMatchingGame.h"

//the 'private' area
@interface CardGameViewController ()

//weak so if view ever went away, this pointer would too. No need to keep
//IBOutlet is nothingness, just typdef to blank but shows
//that it is an outlet...ignored by compiler. lets' you
//it has connections
//remember PROPERTY is a setter and getter for an instance var
@property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
@property (nonatomic) int flipCount; //keeps track of flip count
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
//unordered when creating this array
//IBOutletCollection, void but xcode want sto read it anyways-typdefed to nothingness
//xcode uses these words to keep track of whats going on, that's it

//@property (strong, nonatomic) Deck *deck; //because playingcarddeck inherits from deck,gonna use model instead
//using deck because nothing in this class is going to use anything about a playing card
//makes this class more generic which is good OOP
@property (strong, nonatomic) CardMatchingGame *game;

//ctrl+drag from scorelabel to here to create this outlet
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;


@implementation CardGameViewController

- (Deck *)deck //lazy instantiation of Deck property getter method 
    if (!_deck)
        _deck = [[PlayingCardDeck alloc] init]; //since playingcarddeck inherits
    //from deck, we can use it here instead of 'deck alloc] init'thus it is
    //still a 'Deck' this way through inheritance
    return _deck;

//lazy instantiation of game
- (CardMatchingGame *)game
    //however many card buttons there are, that's how many
    //card are in our game //self.cardbuttons.count
    //getting the card counts from however many buttons are in our view.
    //We already have a property for the deck
    if (!_game) _game = [[CardMatchingGame alloc] initWithCardCount:self.cardButtons.count usingDeck:[[PlayingCardDeck alloc] init]];//self.deck]; BEFORE REMOVING DECK IT WAS THIS <-
    //instead of Deck, we now alloc on the fly as needed (see above line)
    return _game;

//this is the setter method where causing flipping issues
- (void)setCardButtons:(NSArray *)cardButtons //setter for the property/array 
    _cardButtons = cardButtons;
    //instead of commented out code below, we'll us updateUI method
    //which will update the UI by asking the CardMatchingGame what's happening.
    [self updateUI];
     deleting this code that initialized buttons using deck directly.
     Letting CardMatchingGame do that now.
     for (UIButton *cardButton in cardButtons) {
        Card *card = [self.deck drawRandomCard];
        [cardButton setTitle:card.contents forState:UIControlStateSelected];

//where the interface happens
- (void)updateUI
    //To update the UI, we just cycle through
    //the card buttons, getting the associated
    //Card from the CardMatchingGame.
    for (UIButton *cardButton in self.cardButtons)
        //indexOfObject returns lowest index...NSArray method
        //"[" using our Model here
        Card *card = [ cardAtIndex:[self.cardButtons indexOfObject:cardButton]];
        //we set the title in the Select state to be the Card's contents
        //(if the contents have not changed, this will do nothing)
        [cardButton setTitle:card.contents forState:UIControlStateSelected];
        //we set the title when the button is both selected
        //and disabled to ALSO be the Card's contents.
        //A button shows its Normal title whenever it is in a state
        //(or combo of states) for which you have not set a title.
        //Our buttons' Normal title is the Apple logo (the back of the card).
        [cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled];
        //and we select the card only if it isFaceUp
        cardButton.selected = card.isFaceUp;
        //make card untappable if it isUnplayable
        cardButton.enabled = !card.isUnplayable;
        //fix disabled button problem by making them semi-transparent
        //this 1 line--any object in view in iOS can be made transparent
        //by setting this alpha @property. 1.0 = opaque, 0.0 = fully transparent
        //0.3 otherwise 1.0(fully opaque)
        cardButton.alpha = card.isUnplayable ? 0.3 : 1.0;
    //to update score like we did for flips
    self.scoreLabel.text = [NSString stringWithFormat:@"Score: %d",];

//ibaction is typdef of void but for an action
//one arg- the button that sent us the arg
//so we can set that button's selected state
- (IBAction)flipCard:(UIButton *)sender
    //we won't flip cards ourselves anymore. using our Model here
    //We'll let the CardMatchingGame do it. indexOfObject is NSArray method
    [ flipCardAtIndex:[self.cardButtons indexOfObject:sender]];
    //we won't flip cards ourselves anymore; letting cardMatchingGame do it
    //--sender.selected = !sender.isSelected; //when card clicked becomes selected
    /* one liner version above this
     if (sender.isSelected)
     sender.selected = NO;
     sender.selected = YES;
    self.flipCount++; //calls getter and setter in one
    //line like self.flipCount = self.flipCount + 1;
    //when button clicked, flip count ++ed
    //and we have to update the UI whenever a card gets flipped
    [self updateUI];

//implementing setter
- (void)setFlipCount:(int)flipCount
    _flipCount = flipCount; //flipCount gets set
    self.flipsLabel.text = [NSString stringWithFormat:@"Flips: %d", self.flipCount];
    //text is uilabel property..shows flipslabel's text
    //calling setter to set that property and it's setting
    //to string we built


Open in new window

//  CardGameViewController.h
//  Matchismo
//  Created by  on 2/2/13.
//  Copyright (c) 2013 . All rights reserved.

#import <UIKit/UIKit.h>
#import "Card.h"
#import "Deck.h"
#import "PlayingCard.h"
#import "PlayingCardDeck.h"

@interface CardGameViewController : UIViewController


Open in new window

LVL 14

Accepted Solution

binaryevo earned 2000 total points
ID: 38866101
Looks like you can do the same thing to either flipCount or your label ( I think its flipLabel ).  Jusr check and see where you setting either one of those values in your .m file.  Hope this helps.

Author Closing Comment

ID: 38866482
Thank you so much

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at
CodeTwo Sync for iCloud ( automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

606 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question