我有一个 ViewController 和 GameController 类,它只是 NSObject 的子类。视图控制器有一个链接到它的按钮并触发一个 IBAction 来初始化一个 GameController 类。在 GameController 类的 init 中有一个 CADisplayLink,它将一个名为 score 的 int 添加一个。在调试器中,分数在上升,但标签只会显示 0。
在视图控制器中
-(void)setScore:(NSInteger)gameScore{
NSString *string = [NSString stringWithFormat:@"%i", gameScore];
scoreLabel.text = string;
NSLog(@"the score is %i", gameScore);
}
-(IBAction)play:(id)sender{
gameController = [[GameController alloc] init];
//in my header is GameController* gameController;
}
在游戏控制器中
-(id)init{
self = [super init];
if (self) {
viewController = [[ViewController alloc] init];
//in header ViewController* viewController;
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLoop:)];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode: NSRunLoopCommonModes];
//in the header CADisplayLink* displayLink;
}
return self;
}
-(void)gameLoop:(CADisplayLink *)sender{
deltaScore++;
if (deltaScore >= 20) {
deltaScore -= 20;
score++;
//deltaScore and score are NSIntegers
[viewController setScore:score];
}
}