NSTimer使用事項(xiàng)

1.將NSTimer加入NSRunLoopCommonModes避免與主Runloop競(jìng)爭(zhēng)

兩個(gè)Runloop:

  • NSDefaultRunLoopMode: 用于UI的渲染
  • NSRunLoopCommonModes:將NSTimer加入到這個(gè)Runloop,如果不特別聲明,NSTimer會(huì)在默認(rèn)的Runloop運(yùn)行,造成有UISrollView時(shí),滑動(dòng)Scrollview,DefaultRunLoop用于渲染ScrollView,就不會(huì)繼續(xù)NSTimer,NSTimer會(huì)停止。
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                         target:self
                                       selector:@selector(updateLastTime:)
                                       userInfo:nil
                                        repeats:YES];
[[NSRunLoop currentRunLoop]addTimer: timer forMode:NSRunLoopCommonModes];

2.NSTimer使用完之后手動(dòng)銷毀

在使用完NSTimer之后就手動(dòng)調(diào)用[timer invalidate]銷毀NSTimer,不要等到在- (void)dealloc{}方法中銷毀。

- (void)backAction{
    
    [timer invalidate];
    timer = nil;
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • You use the NSTimer class to create timer objects or, mor...
    UILabelkell閱讀 551評(píng)論 1 6
  • 定時(shí)器的用法 系統(tǒng)提供了8個(gè)創(chuàng)建方法,6個(gè)類創(chuàng)建方法,2個(gè)實(shí)例初始化方法。有三個(gè)方法直接將timer添加到...
    gpylove閱讀 1,891評(píng)論 1 3
  • /** * 定義一個(gè)定時(shí)器 */ timer = [NSTimer scheduledTimerWithTimeI...
    錢塘老酒釀閱讀 2,063評(píng)論 0 9
  • 1. 什么是NSTimer ??官方的解釋“A timer provides a way to perform a...
    Timir閱讀 958評(píng)論 0 3
  • 伊川縣文正中學(xué) 昌應(yīng)戰(zhàn) 時(shí)光匆匆,不覺已在文正中學(xué)幸??鞓返毓ぷ魃盍?2個(gè)年頭了。我深深地...
    一葉知秋_0f46閱讀 336評(píng)論 0 0

友情鏈接更多精彩內(nèi)容