我在我的視圖類中每隔15秒調用一次NSTimer
。我的問題是,它工作正常,但我的應用程序變得越來越慢,因爲它正在向整個應用程序顯示其性能。所以我想暫停NSTimer
,當我的視圖從它的超級視圖中消失,並在出現時重新啓動計時器。請幫我解決問題。這裏是我的代碼:如何在視圖消失時暫停並重新啓動NSTimer?
- (void) viewWillAppear:(BOOL)animated
{
if(!Thread_bool)
{
//[spinner startAnimating];
NSTimer *timer_new1=[[NSTimer alloc] init];
timer_new1=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(threadMethod) userInfo:nil repeats:YES];
self.timer_new=timer_new1;
[timer_new1 release];
[self.tableView setEditing:NO];
isEditing=NO;
Thread_bool=YES;
}
}
-(void)viewWillDisappear:(BOOL)animated
{
[self.timer_new invalidate];
timer_new=nil;
}
可能重複的[暫停NSTimer](http://stackoverflow.com/questions/5862743/pausing-an-nstimer) – 2011-05-18 18:49:38