我正在尝试为 iOS 学习 Objective-C。我目前正在关注 iTunesU 上的“一起编码”。虽然我被卡住了,因为我无法让我的 Controller 调用另一个类的方法。找不到我做错了什么,并认为 StackOverflow 可能有解决方案!
方法“flipCardAtIndex”是不起作用的方法。我已经使用 nslog 进行了调试,并从方法“flipCard”中得到了一个输出。但是当我为 FlipCardAtIndex 执行实现时,我什么也没有得到。所以我的猜测是它永远不会调用它...
我将代码缩短了一些,所以它只是我认为重要的部分,这是 Controller :
#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];
}
最佳答案
使固定?
- (CardMatchingGame *) game{
if (!_game) _game = [[CardMatchingGame alloc] initWithCardCount:[self.cardButtons count] usingDeck:[[PlayingCardDeck alloc] init]];
return _game;
}
关于ios - 不会调用其他类中的方法,Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14738901/