随机更改UIButton(在本例中为扑克牌)的标题

时间:2013-01-29 17:55:50

标签: iphone objective-c ios6 uibutton

ios编程的新手,所以对于练习我试图制作一个带有扑克牌的简单应用程序,它可以回到视图上,你可以点击它来显示卡片的正面。每次点击它时,卡的正面都不同。 我想要实现的是当我点击卡片时:

  1. 翻转并显示随机卡片,
  2. 有一个标签,每次翻转卡片时递增1
  3. 我完成了第二项要求,但我似乎无法让第一项要求正确。我有3个型号(卡,甲板,PlayCard) 我的Deck模型负责随机化卡片,这里是代码。

    加入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
    

    我的PlayCard基本上描述了卡的内容。例如。杰克的心,2个钻石杰克将成为卡片的“等级”和“心”将是卡片的套装,它们共同构成卡片的内容。

    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
    

    最后我的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
    

    我研究了这个,我发现我应该使用

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

    其中状态为UIControlStateSelected,因为包含内容的卡的正面是按钮的选定内容,但我不知道如何根据我的模型开发的随机卡内容设置标题。

2 个答案:

答案 0 :(得分:1)

让我们假设你的CardGameViewController中有一个Deck类(称为cardDeck)的属性,并且你已经使用addCard方法用卡填充了该属性。 现在,当用户点击翻转按钮时,你应该做这样的事情::

- (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 ++;         
  }
}

希望这有帮助

PS :: drawRandomCard方法有一个返回类型“Card”,它似乎应该是“PlayingCard”,或者PlayCard类应该被命名为Card。

答案 1 :(得分:0)

您没有初始化您的扑克牌。您的ViewController.m文件应该如下所示(我在flipCard方法中显示所有卡后我也重置了套牌):

    #import "CardViewViewController.h"
    #import "PlayingCardDeck.h"

    @interface CardViewViewController ()
    @property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
    @property (nonatomic) int flipCount;
    @property (nonatomic) PlayingCardDeck *myDeck;
    @end
    @implementation CardViewViewController
    -(PlayingCardDeck *)myDeck
    {
        if(!_myDeck) _myDeck=[[PlayingCardDeck alloc] init];
        return _myDeck;
    }

    -(void)setFlipCount:(int)flipCount
    {
        _flipCount= (int) flipCount;
        self.flipsLabel.text=[NSString stringWithFormat:@"Flips: %d",self.flipCount];
    }
    - (IBAction)flipCard:(UIButton *)sender
    {
        if (!sender.isSelected)
        {
            if (self.flipCount >= 52)
            {
                self.flipCount = 0;
                self.myDeck = nil;
            }
            Card *cCard = [self.myDeck drawRandomCard];
            [sender setTitle: [cCard contents] forState:UIControlStateSelected];
            self.flipCount++;
        }
        sender.selected = !sender.isSelected;
    }

@end