gpt4 book ai didi

iphone - 如何让我的段 UISegmentedControl 响应?

转载 作者:行者123 更新时间:2023-11-28 22:37:44 26 4
gpt4 key购买 nike

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

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

据我所知,最好用枚举定义这些变量,这是我在 Controller 的顶部所做的,但似乎我在其中遗漏了一些东西..

抱歉,如果它不是那么直接,但我的 Controller 非常简短,如果你能告诉我我在 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:

关于iphone - 如何让我的段 UISegmentedControl 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15438625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com