2013-02-06 23 views
1

我正在尝试为iOS学习Objective-C。我目前正在跟随iTunesU上的“一起编码”。虽然我陷入困境,因为我无法让我的控制器调用另一个类的方法。找不到我做错了什么,并认为StackOverflow可能有解决方案!不会调用其他类中的方法Objective-C

“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

'CardCardAtIndex'在CardMatchingGame.h中定义了吗? –

+0

顺便说一句,当你说'和实现'时,我假设你正在从你的CardMatchingGame类拉代码? –

+0

@JamesBoutcher声明的存在如何影响任何东西? – 2013-02-06 21:18:21

回答

4

修复?

- (CardMatchingGame *) game{ 
    if (!_game) _game = [[CardMatchingGame alloc] initWithCardCount:[self.cardButtons  count] usingDeck:[[PlayingCardDeck alloc] init]]; 
    return _game; 
} 
+0

嗯,我认为这是问题,但我现在我在其他地方崩溃.. – ZedIsDead

+0

嗯,这个问题得到了答复;-) –

+0

感谢您的帮助! :) – ZedIsDead

相关问题