如何让我的段UISegmentedControl响应?

时间:2013-03-15 17:29:28

标签: iphone ios objective-c cocoa-touch

在我的ViewController中,我添加了一个UISegmentedControl来为不同的Segmented Control选择执行不同的任务。它是一款卡片匹配游戏。

一切似乎都运行得很好,除了分段控件没有对选择做出反应..我创建了一个开关,以便在“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

2 个答案:

答案 0 :(得分:1)

好吧,我没有看到任何addTarget:调用分段控件。所以你可能在Interface Builder中设置它们。检查IB连接。如果IB中的所有内容都显示正常 - 请以编程方式addTarget:尝试。

答案 1 :(得分: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];
}

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