簡體   English   中英

如何讓我的段UISegmentedControl響應?

[英]How do I get my segment UISegmentedControl to respond?

在我的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

好吧,我沒有看到任何addTarget:調用分段控件。 所以你可能在Interface Builder中設置它們。 檢查IB連接。 如果一切都在IB似乎確定-嘗試addTarget:編程。

我認為你最好創建一個選擇器並將其添加到分段控制目標,如下所示:

[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];
}

這應該工作正常。 目前使用類似的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM