-3

'NSArray' 没有可见的@interface 声明选择器'setTitle:forState:'

当我运行应用程序时,我发现只有一个错误,错误是 No visible @interface for 'NSArray' 声明选择器 'setTitle:forState:'

这是我的代码

< #import "CardGameViewController.h"
 #import "PlayingCardDeck.h" 

@interface CardGameViewController ()
@property (weak, nonatomic) IBOutlet UILabel *flipslabel;
@property(nonatomic) int flipCount;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@property (strong, nonatomic) Deck *deck;
@end

@implementation CardGameViewController

-(void)setCardButtons:(NSArray *)cardButtons
{
    _cardButtons = cardButtons;
for (UIButton *cardButton in self.cardButtons){
    Card *card = [self.deck drawRandomCard];
    [cardButtons setTitle:card.contents forState:UIControlStateSelected];
}
}


- (Deck *)deck
{
if(!_deck) _deck=[[PlayingCardDeck alloc] init];
return _deck;    
}



- (void)setFlipCount:(int)flipCount
 {

_flipCount = flipCount;
self.flipslabel.text= [NSString stringWithFormat:@"Flips: %d", self. flipCount];

}


- (IBAction)flipCard:(UIButton *)sender
{

sender.selected=!sender.isSelected;
self.flipCount++;
}

@end

你认为错误是什么?

4

4 回答 4

2

您的循环似乎关闭了,您正在遍历数组中的按钮并尝试设置数组的标题,而不是按钮;

for (UIButton *cardButton in self.cardButtons){
    Card *card = [self.deck drawRandomCard];
    [cardButtons setTitle:card.contents forState:UIControlStateSelected];
  // ^^^^^^^^^^^ should be cardButton
}
于 2013-08-11T21:20:13.393 回答
1

你的 for 循环中有一个错字。您需要在循环中引用变量“cardButton”,而不是数组“cardButtons”。

所以,从这

[cardButtons setTitle:card.contents forState:UIControlStateSelected];

对此:

[cardButton setTitle:card.contents forState:UIControlStateSelected];

这可能只是您错过的自动完成错字。

于 2013-08-11T21:21:04.720 回答
0

原来的

-(void)setCardButtons:(NSArray *)cardButtons
{
    _cardButtons = cardButtons;
for (UIButton *cardButton in self.cardButtons){
    Card *card = [self.deck drawRandomCard];
    [cardButtons setTitle:card.contents forState:UIControlStateSelected];
    }
}

固定 -setTitle:forState是一种UIButton方法,你在你的cardButtons数组上调用它

-(void)setCardButtons:(NSArray *)cardButtons
{
    _cardButtons = cardButtons;
for (UIButton *cardButton in self.cardButtons){
    Card *card = [self.deck drawRandomCard];
    [cardButton setTitle:card.contents forState:UIControlStateSelected];
    }
}
于 2013-08-11T21:21:01.267 回答
0

你正在做[cardButtons setTitle:card.contents forState:UIControlStateSelected];,这是调用你创建的 NSArray 上的方法。

你想要的是:

[cardButton setTitle:card.contents forState:UIControlStateSelected];
于 2013-08-11T21:21:44.030 回答