MNY
MNY

Reputation: 1536

How do I get my segment UISegmentedControl to respond?

In my ViewController I have added A UISegmentedControl to preform different task for a different selection of the Segmented Control. Its a cards matching game.

And everything seems to work just fine, except that the Segmented Control is not reacting to the selection.., I created a switch to do something in case of "twoCardGame" and in case of "threeCardGame".

From what I understand it would be good to define those variables with enum, which I did in the top part of the controller, but it seems like i'm missing something in it..

Sorry if its not so directed, but my controller is pretty short and simple, would appreciate if you can tell me what am I doing wrong in term of the UISegmentedControl.

Here it is:

#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

Upvotes: 0

Views: 486

Answers (2)

Tim
Tim

Reputation: 9042

I think you'd be better off creating a selector and adding it to the segmented control target like so:

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

This should work fine. Using similar code myself currently.

Upvotes: 1

folex
folex

Reputation: 5287

Well, I don't see any addTarget: calls to segmented control. So you probably set them in Interface Builder. Check IB connections. If everything in IB seems ok -- try to addTarget: programmatically.

Upvotes: 1

Related Questions