0
我想在调用viewWillAppear
方法时启动计时器。计时器无法启动viewWillAppear方法
它的工作完美,但当我在这个当前视图上推出一个新视图时,有一个问题,它已经有一个计时器。当我弹出新的视图并调用viewWillAppear
时,我想再次启动计时器。我拥有大部分技术,但无法找到解决方案。
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[NSThread detachNewThreadSelector:@selector(createTimer) toTarget:self withObject:self];
}
- (void)createTimer
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
gameTimer = [[NSTimer timerWithTimeInterval:1.00 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES] retain];
[[NSRunLoop currentRunLoop] addTimer:gameTimer forMode:NSDefaultRunLoopMode];
timeCount = 360;
[runLoop run];
[pool release];
}
- (void)timerFired:(NSTimer *)timer
{
if(timeCount == 0)
{
[self timerExpired];
} else {
timeCount--;
if(timeCount == 0) {
[timer invalidate];
[self timerExpired];
}
}
timeLabel.text = [NSString stringWithFormat:@"%02d:%02d",timeCount/60, timeCount % 60];
}
- (void) timerExpired
{
//NSLog(@"Final == %@",arrayAnswers);
//NSLog(@"Attempt == %d",[arrayAnswers count]);
[gameTimer invalidate];
gameTimer = nil;
}
如果您[显示**代码的相关**部分](http://mattgemmell.com/2008/12/08/what-have-you-tried/)以便人们可以找出你的问题在哪里。 – Ben 2012-07-07 11:04:41
你尝试过'[timer invalidate];'? – Bazinga 2012-07-07 11:10:02
我尝试了所有可能的方式,但我仍然无法解决问题 – iDhaval 2012-07-07 11:15:51