我试图学习obj-c,事实是,按照教程,我发现自己 " CardGameViewer没有可见的@interface声明选择器' titleForCard'"和 " CardGameViewer没有可见的@interface声明选择器&#background; backgroundImageForCard'" 我不懂。是因为" self"可能没有参考好东西?
提前致谢。
//CardGameViewController.m
#import "CardGameViewController.h"
#import "PlayingCardDeck.h"
#import "CardMatchingGame.h"
@interface CardGameViewController ()
@property (strong, nonatomic)Deck *deck;
@property (strong, nonatomic)CardMatchingGame *game;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@end
@implementation CardGameViewController
-(Deck *)createDeck{
return [[PlayingCardDeck alloc] init];
}
-(CardMatchingGame *)game{
if(!_game) _game= [[CardMatchingGame alloc]initWitchCardCount:[self.cardButtons count] usingDeck:[self createDeck]];
return _game;
}
- (IBAction)touchCardButton:(UIButton *)sender {
int cardIndex = [self.cardButtons indexOfObject:sender];
[self.game chooseCardAtIndex:cardIndex];
[self updateUI];
}
-(void)updateUI{
for (UIButton *cardButton in self.cardButtons){
int cardIndex = [self.cardButtons indexOfObject:cardButton];
Card *card= [self.game cardAtIndex:cardIndex];
[cardButton setTitle:**[self titleForCard]** forState:(UIControlStateNormal)]; //ERROR HERE
[cardButton setBackgroundImage:**[self backgroundImageForCard]** forState: (UIControlStateNormal)]; //ERROR HERE
cardButton.enabled = !card.isMatched;
}
}
- (NSString *)titleForCard:(Card *)card{
return card.isChosen ? card.contents : @"";
}
- (UIImage *)backgroundImageForCard:(Card *)card{
return [UIImage imageNamed:card.isChosen ? @"cardFront" : @"cardBack"];
}
@end
答案 0 :(得分:2)
这些方法有参数。
您应该像以下一样使用它们:
[cardButton setTitle:[self titleForCard:card] forState:(UIControlStateNormal)];
[cardButton setBackgroundImage:[self backgroundImageForCard:card] forState: (UIControlStateNormal)];
P.S:自我很好,因为你在“自我”中宣称这些方法。