Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:58:06+00:00 2026-06-18T06:58:06+00:00

New to ios programming so for practice I am trying to make a simple

  • 0

New to ios programming so for practice I am trying to make a simple app with a playing card with it’s back on the view which you can tap to reveal the front of the card. The front of the card is different every time you tap it.
What I am trying to achieve is that when i tap the card:

  1. it flips and displays a random card,
  2. have a label that increments by 1 every time the card is flipped

I am done with the second requirement but I can’t seem to get the first one right. I have 3 models(Card, Deck, PlayingCard)
My Deck model is responsible for randomising the cards and here are the codes.

Deck.h

#import <Foundation/Foundation.h>
#import "Card.h"

@interface Deck : NSObject
- (void)addCard:(Card *)card atTop:(BOOL)atTop;

- (Card *)drawRandomCard;

@end

Deck.m

#import "Deck.h"
@interface Deck()

@property (strong, nonatomic) NSMutableArray *cards;

@end

@implementation Deck
-(NSMutableArray *)cards
{
    if(!_cards) _cards =[[NSMutableArray alloc] init];
    return _cards;
} 

- (void)addCard:(Card *)card atTop:(BOOL)atTop
{
    if(card){
        if(atTop){
            [self.cards insertObject:card atIndex:0];
        }
        else
        {
            [self.cards addObject:card];
        }
    }
} 

- (Card *)drawRandomCard
{
    Card *randomCard = nil;  

    if(self.cards.count){
        unsigned index = arc4random() % self.cards.count;
        randomCard = self.cards[index];
        [self.cards removeObjectAtIndex:index];
    }

    return randomCard;
}
@end

My PlayingCard basically describes the contents of a Card. for eg. Jack of Hearts, 2 of Diamonds where Jack would be the “rank” of the card and “hearts” would be the suit of the card which together form the contents of the card.

PlayingCard.h

#import <Foundation/Foundation.h>
#import "Card.h"
@interface PlayingCard : NSObject

@property (strong, nonatomic) NSString *suit;
@property (nonatomic) NSUInteger   rank;

+ (NSArray *) validSuits;
+ (NSUInteger) maxRank;

- (NSString *)contents;
@end

PlayingCard.m

#import "PlayingCard.h"

@implementation PlayingCard

- (NSString *)contents
{
    NSArray *rankStrings = [PlayingCard rankStrings];
    return [rankStrings[self.rank] stringByAppendingString:self.suit];
}

@synthesize suit = _suit;

+(NSArray *)validSuits{
    return @[@"♥", @"♦", @"♠", @"♣"] ;
}

+(NSArray *)rankStrings{
    return  @[@ "?", @"A", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9",@"10", @"J", @"Q", @"K"];

}
-(void)setSuit:(NSString *)suit
{
    if([[PlayingCard validSuits] containsObject:suit]){
        _suit = suit;
    }
}

-(NSString *)suit{
    return _suit ? _suit: @"?";
}

+ (NSUInteger)maxRank {
    return [self rankStrings].count-1;
}

-(void)setRank:(NSUInteger)rank{
    if(rank <= [PlayingCard maxRank]) {
        _rank = rank;
    }
}
@end

Finally my ViewController.m

#import "CardGameViewController.h"
#import "Card.h"
#import "Deck.h"
#import "PlayingCard.h"

@interface CardGameViewController ()
@property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
@property (nonatomic) int flipCount;
@property (nonatomic) NSString *title;
@property (weak, nonatomic) IBOutlet UIButton *cardRandom;
@end

@implementation CardGameViewController

- (void)setFlipCount:(int)flipCount
{
    _flipCount = flipCount;
    self.flipsLabel.text = [NSString stringWithFormat:@" Flips:%d", self.flipCount ];
}

- (IBAction)flipCard:(UIButton *)sender
{

    if(sender.isSelected) {
        sender.selected = NO;
        self.flipCount ++;
    }
    else{
        sender.selected = YES;
        self.flipCount ++;         
    }
}

@end

I researched about this and i found that I should probably use

- (void)setTitle:(NSString *)title forState:(UIControlState)state

where the state will be UIControlStateSelected because the front of the card containing the contents is the selected content of the button but I dont know how to set the title according to the random card content developed by my model.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:58:08+00:00Added an answer on June 18, 2026 at 6:58 am

    Lets assume you have a property of the class Deck (called cardDeck) in your CardGameViewController, and you have already populated that property with cards using addCard method.
    Now when the user taps flip button, you should do something like this ::

    - (IBAction)flipCard:(UIButton *)sender
    {
    
      //draw a random card
      Card *randomCard = [self.cardDeck drawRandomCard];
    
      //set the button title
      [sender setTitle:[randomCard contents] forState:UIControlStateSelected];
    
      if(sender.isSelected) {
        sender.selected = NO;
        self.flipCount ++;
      }
      else{
        sender.selected = YES;
        self.flipCount ++;         
      }
    }
    

    Hope this helps

    PS :: drawRandomCard method has a return type “Card”, it seems it should be “PlayingCard”, or the PlayingCard class should be named Card.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im pretty new at iOS programming, I'm trying to make this budget app. I
Somewhat new to ios programming. Trying to use AVQueuePlayer and playing multiple videos in
I am fairly new to iOS programming and I'm trying to make an iOS
Im new to iOS programming and I'm trying to get the table view running
I'm a new iOS programming and I'm developing a simple iPhone game that needs
i'm just new to iOS Programming, so appreciate for any help. I'm trying to
I am new to iOS programming.Can you please tell me how to convert the
I am fairly new to Objective C and iOS programming but am constantly trying
I have a simple iOS app in which one class called Dictionary (.m and
I am relatively new to iOS and programming, and I made an app before,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.