码迷,mamicode.com
首页 > 其他好文 > 详细

取消 NSTimer 和 performSelector

时间:2014-08-27 20:14:28      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:blog   使用   io   for   ar   div   log   sp   on   

使用 NSTimer 和 perfrormSelectorOnMainThread:withObject:waitUntilDone 做定时并可以取消移除:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self performSelectorOnMainThread:@selector(autoRefreshRecentDoctorAdvices) withObject:nil waitUntilDone:NO];
}

//自动刷新
-(void)autoRefreshRecentDoctorAdvices
{
    //_timer 是对象的私有变量,以便取消定时
    _timer = [NSTimer scheduledTimerWithTimeInterval:100 target:self selector:@selector(getRecentDoctorAdvices) userInfo:nil repeats:YES];
}

//取消定时,并移除 performSelector
-(void)   cancelTimer
{
   if( _timer)
    {
        [_timer invalidate];
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoRefreshRecentDoctorAdvices) object:nil];
        return;
    }  
}

  

取消 NSTimer 和 performSelector

标签:blog   使用   io   for   ar   div   log   sp   on   

原文地址:http://www.cnblogs.com/1oo1/p/3940090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!