Solved

"Restart" the game or app in xcode / iOS

Posted on 2013-02-06
4
2,680 Views
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?
0
Comment
Question by:Mark_Co
  • 2
  • 2
4 Comments
 
LVL 14

Assisted Solution

by:binaryevo
binaryevo earned 500 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


or

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...
0
 

Author Comment

by:Mark_Co
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

//NO LONGER NEED OUR DECK PROPERTY SINCE WE AREN'T INIT USING DECK DIRECTLY
//@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;

@end

@implementation CardGameViewController


/* REMOVING SINCE WE NO LONGER NEED OUR DECK PROPERTY
- (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
        //"[self.game..." using our Model here
        Card *card = [self.game 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", self.game.score];
    
    
}

//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. self.game: using our Model here
    //We'll let the CardMatchingGame do it. indexOfObject is NSArray method
    [self.game 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;
     }
     else
     {
     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
}





@end

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

@end

Open in new window

0
 
LVL 14

Accepted Solution

by:
binaryevo earned 500 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.
0
 

Author Closing Comment

by:Mark_Co
ID: 38866482
Thank you so much
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When Apple released Swift last year, the aim was to introduce a new programming language for Cocoa and Cocoa Touch that was fast, easy and effective, like the name connotes. Apple succeeded. Swift is designed to couple with Objective-C program…
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
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 http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now