[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2970
  • Last Modified:

"Restart" the game or app in xcode / iOS

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
Mark_Co
Asked:
Mark_Co
  • 2
  • 2
2 Solutions
 
binaryevoCommented:
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
 
Mark_CoAuthor Commented:
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
 
binaryevoCommented:
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
 
Mark_CoAuthor Commented:
Thank you so much
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now