NSTimer使用事项

2019-08-19  本文已影响0人  快乐捣蛋鬼

1.将NSTimer加入NSRunLoopCommonModes避免与主Runloop竞争

两个Runloop:

timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                         target:self
                                       selector:@selector(updateLastTime:)
                                       userInfo:nil
                                        repeats:YES];
[[NSRunLoop currentRunLoop]addTimer: timer forMode:NSRunLoopCommonModes];

2.NSTimer使用完之后手动销毁

在使用完NSTimer之后就手动调用[timer invalidate]销毁NSTimer,不要等到在- (void)dealloc{}方法中销毁。

- (void)backAction{
    
    [timer invalidate];
    timer = nil;
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
上一篇下一篇

猜你喜欢

热点阅读