0

在我的 ViewController 中,我添加了一个 UISegmentedControl 来执行不同的任务来选择不同的分段控件。它是一款纸牌配对游戏。

一切似乎都很好,除了分段控件没有对选择做出反应..,我创建了一个开关来在“twoCardGame”和“threeCardGame”的情况下做一些事情。

据我了解,用枚举定义这些变量会很好,我在控制器的顶部做了这些,但似乎我在其中遗漏了一些东西..

抱歉,如果它不是那么直接,但我的控制器非常简短,如果你能告诉我我在 UISegmentedControl 方面做错了什么,我将不胜感激。

这里是:

#import "CardGameViewController.h"
#import "PlayingCardsDeck.h"
#import "CardMatchingGame.h"

enum CardGame {
    twoCardGame,
    threeCardGame
};

@interface CardGameViewController ()


@property (weak, nonatomic) IBOutlet UILabel *notificationLabel;
@property (weak, nonatomic) IBOutlet UILabel *scoreCounter;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@property (strong, nonatomic) CardMatchingGame *game;
@property (weak, nonatomic) IBOutlet UISegmentedControl *numberOfCardsToPlayWith;

@end


@implementation CardGameViewController


//creating the getter method that creates a new card game.
- (CardMatchingGame *)game {
    if (!_game) {
        _game = [[CardMatchingGame alloc] initWithCardCount:self.cardButtons.count
                                                  usingDeck:[[PlayingCardsDeck alloc] init]];
        _game.numberOfCardsToPlayWith  = [self selectNumberOfCardsToPlayWith];
    }
    return _game;
}


//creating a setter for the IBOutletCollection cardButtons
-(void)setCardButtons:(NSArray *)cardButtons {
    _cardButtons = cardButtons;
    [self updateUI];
}



- (void)updateUI {

    for (UIButton *cardButton in self.cardButtons) {
        Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]];
        [cardButton setTitle:card.contents forState:UIControlStateSelected];
        [cardButton setTitle:card.contents
                    forState:UIControlStateSelected|UIControlStateDisabled];
        cardButton.selected = card.isFaceUp;
        cardButton.enabled = !card.isUnplayable;
        cardButton.alpha = card.isUnplayable ? 0.3 : 1.0;

    }
    self.scoreCounter.text = [NSString stringWithFormat:@"Score: %d", self.game.score];
}


//Here I created a method to flipCards when the card is selected, and give the user a random card from the deck each time he flips the card. After each flip i'm incrementing the flipCount setter by one.
- (IBAction)flipCard:(UIButton *)sender {

    [self.game flipCardAtIndex:[self.cardButtons indexOfObject:sender]];;
    [self updateUI];
}


//sending an alert if the user clicked on new game button
- (IBAction)newGame:(UIButton *)sender {

   UIAlertView* mes=[[UIAlertView alloc] initWithTitle:@"Think about it for a sec..?" message:@"This will start a new game" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];

    [mes show];
}


- (NSUInteger)selectNumberOfCardsToPlayWith {
    switch (self.numberOfCardsToPlayWith.selectedSegmentIndex) {
        case twoCardGame:
            return 2;
        case threeCardGame:
            return 3;
        default:
            return 2;
    }

    [self updateUI];
}

//preforming an action according to the user choice for the alert yes/no to start a new game
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

    if (buttonIndex != [alertView cancelButtonIndex]) {

        self.game = nil;
        for (UIButton *button in self.cardButtons) {
            Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:button]];
            card.isUnplayable = NO;
            card.isFaceUp = NO;
            button.alpha = 1;
        }

        self.notificationLabel.text = nil;
        [self updateUI];

    }
}


@end
4

2 回答 2

1

我认为您最好创建一个选择器并将其添加到分段控制目标中,如下所示:

[segmentedControl addTarget:self
                         action:@selector(selectNumberOfCardsToPlayWith:)
               forControlEvents:UIControlEventValueChanged];

- (NSUInteger)selectNumberOfCardsToPlayWith:(UISegmentedControl *)control {
        switch (control.selectedSegmentIndex) {
            case twoCardGame:
                return 2;
            case threeCardGame:
                return 3;
            default:
                return 2;
        }

        [self updateUI];
}

这应该可以正常工作。目前我自己使用类似的代码。

于 2013-03-15T17:43:16.003 回答
1

好吧,我没有看到任何addTarget:对分段控制的调用。所以你可能在 Interface Builder 中设置它们。检查 IB 连接。如果 IB 中的一切看起来都不错——尝试以addTarget:编程方式。

于 2013-03-15T17:38:23.440 回答