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];
}