我正在尝试为iOS学习Objective-C。我目前正在关注iTunesU上的“编码”。虽然我已经卡住了,因为我无法让我的控制器调用另一个类的方法。无法找到我做错了什么,并认为StackOverflow可能有解决方案!
方法“flipCardAtIndex”是不起作用的方法。我已经使用nslog进行了调试,并从方法“flipCard”获得输出。但是当我为flipCardAtIndex实现时,我没有得到任何东西..所以我的猜测是它永远不会叫它...... 我把代码缩短了一点,所以我认为只有部分是重要的,这是控制器:
#import "ViewController.h"
#import "PlayingCardDeck.h"
#import "CardMatchingGame.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
@property (nonatomic) int flipCount;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@property (strong, nonatomic) CardMatchingGame *game;
@end
@implementation ViewController
- (CardMatchingGame *) game{
if (_game) _game = [[CardMatchingGame alloc] initWithCardCount:[self.cardButtons count]
usingDeck:[[PlayingCardDeck alloc] init]];
return _game;
}
- (IBAction)flipCard:(UIButton *)sender {
[self.game flipCardAtIndex:[self.cardButtons indexOfObject:sender]];
self.flipCount++;
[self updateUI];
}
并实施:
- (void)flipCardAtIndex:(NSUInteger)index
{
NSLog(@"ALL YOUR BASE ARE BELONG TO US");
Card *card = [self cardAtIndex:index];
}
答案 0 :(得分:4)
修正?
- (CardMatchingGame *) game{
if (!_game) _game = [[CardMatchingGame alloc] initWithCardCount:[self.cardButtons count] usingDeck:[[PlayingCardDeck alloc] init]];
return _game;
}