NSTimer用法

Timers的替代方法

如果只是要延迟消息的发送,可以使用NSObject的方法

 

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget

 

创建Timer的三种方法

1.scheduling a timer with the current run loop 

2.creating a timer that you later register with a run loop

3.initializing a timer with a given fire date

 

 

Scheduled Timers

以下两个方法自动注册新创建的timer到当前NSRunLoop对象,NSRunLoop的模式为默认的NSDefaultRunLoopMode

  • + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation *)invocation repeats:(BOOL)repeats
  • + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInforepeats:(BOOL)repeats
 
只发送一次
[plain]  view plain copy print ?
  1. - (IBAction)startOneOffTimer:sender {  
  2.    
  3.     [NSTimer scheduledTimerWithTimeInterval:2.0  
  4.              target:self  
  5.              selector:@selector(targetMethod:)  
  6.              userInfo:[self userInfo]  
  7.              repeats:NO];  
  8. }  

 

重复发送消息

注:创建重复发送消息的timer一般需要保存一个引用,因为需要在某个时刻停止发送消息

[plain]  view plain copy print ?
  1. - (IBAction)startRepeatingTimer:sender {  
  2.    
  3.     NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5  
  4.                               target:self selector:@selector(timerFireMethod:)  
  5.                               userInfo:[self userInfo] repeats:YES];  
  6.     self.repeatingTimer = timer;  
  7. }  



 

Unscheduled Timers

创建未注册的timer,使用时调用addTimer:forMode注册到NSRunLoop对象

  • timerWithTimeInterval:target:selector:userInfo:repeats:
  • timerWithTimeInterval:invocation:repeats:
[plain]  view plain copy print ?
  1. - (IBAction)createUnregisteredTimer:sender {  
  2.    
  3.     NSMethodSignature *methodSignature = [self methodSignatureForSelector:@selector(invocationMethod:)];  
  4.     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];  
  5.     [invocation setTarget:self];  
  6.     [invocation setSelector:@selector(invocationMethod:)];  
  7.     NSDate *startDate = [NSDate date];  
  8.     [invocation setArgument:&startDate atIndex:2];  
  9.    
  10.     NSTimer *timer = [NSTimer timerWithTimeInterval:0.5 invocation:invocation repeats:YES];  
  11.     self.unregisteredTimer = timer;  
  12. }  

 

[plain]  view plain copy print ?
  1. - (IBAction)startUnregisteredTimer:sender {  
  2.     if (unregisteredTimer != nil) {  
  3.         NSRunLoop *runLoop = [NSRunLoop currentRunLoop];  
  4.         [runLoop addTimer:unregisteredTimer forMode:NSDefaultRunLoopMode];  
  5.     }  
  6. }  

 

Initializing a Timer with a Fire Date

创建一个拥有指定发送日期的timer

 

[plain]  view plain copy print ?
  1. - (IBAction)startFireDateTimer:sender {  
  2.     NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:1.0];  
  3.     NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate  
  4.                                       interval:0.5  
  5.                                       target:self  
  6.                                       selector:@selector(countedtargetMethod:)  
  7.                                       userInfo:[self userInfo]  
  8.                                       repeats:YES];  
  9.    
  10.     timerCount = 1;  
  11.     NSRunLoop *runLoop = [NSRunLoop currentRunLoop];  
  12.     [runLoop addTimer:timer forMode:NSDefaultRunLoopMode];  
  13.     [timer release];  
  14. }  

 

 

Stopping a Timer

[plain]  view plain copy print ?
  1. - (IBAction)stopRepeatingTimer:sender {  
  2.     [repeatingTimer invalidate];  
  3.     self.repeatingTimer = nil;  
  4. }  
  5.    
[plain]  view plain copy print ?
  1. 也可以从timer发送的消息中停止timer  
[plain]  view plain copy print ?
  1. - (void)countedtargetMethod:(NSTimer*)theTimer {  
  2.    
  3.     NSDate *startDate = [[theTimer userInfo] objectForKey:@"StartDate"];  
  4.     NSLog(@"Timer started on %@; fire count %d", startDate, timerCount);  
  5.    
  6.     timerCount++;  
  7.     if (timerCount > 3) {  
  8.         [theTimer invalidate];  
  9.     }  
  10. }  



Memory Management

1. The run loop maintains the timer that is registered to it.

2. The timer is passed as an argument when you specify its method as a selector

3. You should maintain a strong reference to the unscheduled timer, in order to ensure that it's not deallocated before you use it.

4. A timer maintains a strong reference to its user info dictionary,

5. A timer maintains a strong reference to its target, so you should make sure that your timer's target survive longer than the timer itself.

转载于:https://www.cnblogs.com/lixiong-iOS/p/3559671.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
校园失物招领系统管理系统按照操作主体分为管理员和用户。管理员的功能包括字典管理、论坛管理、公告信息管理、失物招领管理、失物认领管理、寻物启示管理、寻物认领管理、用户管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 校园失物招领系统管理系统可以提高校园失物招领系统信息管理问题的解决效率,优化校园失物招领系统信息处理流程,保证校园失物招领系统信息数据的安全,它是一个非常可靠,非常安全的应用程序。 ,管理员权限操作的功能包括管理公告,管理校园失物招领系统信息,包括失物招领管理,培训管理,寻物启事管理,薪资管理等,可以管理公告。 失物招领管理界面,管理员在失物招领管理界面中可以对界面中显示,可以对失物招领信息的失物招领状态进行查看,可以添加新的失物招领信息等。寻物启事管理界面,管理员在寻物启事管理界面中查看寻物启事种类信息,寻物启事描述信息,新增寻物启事信息等。公告管理界面,管理员在公告管理界面中新增公告,可以删除公告。公告类型管理界面,管理员在公告类型管理界面查看公告的工作状态,可以对公告的数据进行导出,可以添加新公告的信息,可以编辑公告信息,删除公告信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值