我有一个类似 GameScreen.m 的文件(这是一段简化的代码):
- (IBAction) onCellClick:(id) sender
{
points +=1;
self.myScore.text = [[NSNumber numberWithInt: points] stringValue];
//myScore is a label in GameScreenViewController xib
}
也就是说,在单击视图中的单元格时,它会将文本标签增加 1。到目前为止一切顺利。
然后,在相同的代码中,我有一个计时器:
- (void) startTimer
{
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(updateCounter:)
userInfo:nil
repeats:YES];
}
它的 updateCounter 方法是:
- (void) updateCounter:(NSTimer *)theTimer
{
int seconds;
static int count = 0;
count +=1;
timeElapsed = [[NSString alloc] initWithFormat:@"%d", seconds + count];
self.time.text = timeElapsed;
//time is a label in GameScreenViewController xib
}
问题是在这种情况下“时间”标签没有更新(每次 1 秒)。我插入了一个 AlertView 来检查 startTimer 方法是否有效并正确调用,它实际上是(它每秒显示一个恼人的 alertview 和 timeElapsed 值)。但是,我无法更改要更改的时间标签值。
为什么我的分数标签在操作时更新,而时间标签不是每秒更新一次?有什么方法可以在不将我的代码包含在 ViewController 中的情况下更新它?
//注意:我的代码分为三个文件:appDelegate 翻转屏幕并在它们之间发送值;我的 viewControllers 只是窗口,最后,我的 GameScreen 类管理所有进程。在 xib 中,File's Owner 连接到 ViewController,而视图连接到 GameScreen 类。
非常感谢您的任何反馈,请随时询问所需的任何附加代码。