我有一個ViewController和GameController類,它只是從NSObject子類。視圖控制器有一個按鈕鏈接到它並觸發一個位於GameController類中的IBAction。在GameController類的init中有一個CADisplayLink,它將一個int加入到一個名爲score的int中。在調試器中的得分上升,但標籤將在GameController爲什麼通過我的UILabel顯示一個NSInteger
-(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;
}
在ViewController中只顯示爲0。
-(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];
}
}
你在哪裏添加'viewController'的視圖? –