创建一个 Timer

  • + scheduledTimerWithTimeInterval: invocation: repeats:
  • + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti   invocation:(NSInvocation *)invocation   repeats:(BOOL)yesOrNo;
  • + scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:
  • + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti   target:(id)aTarget   selector:(SEL)aSelector   userInfo:(id)userInfo   repeats:(BOOL)yesOrNo;
  • 创建返回一个新的NSTimer对象和时间表,在当前的默认模式下循环调用一个实例方法。
  • + timerWithTimeInterval: invocation: repeats:
  • + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
  • + timerWithTimeInterval: target:selector: userInfo:repeats:
  • + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
  • – initWithFireDate: interval: target: selector: userInfo: repeats:
  • - (id)initWithFireDate:(NSDate *)date   interval:(NSTimeInterval)ti     target:(id)t    selector:(SEL)s    userInfo:(id)ui    repeats:(BOOL)rep;

scheduledTimerWithTimeInterval:(NSTimeInterval)seconds

预订一个Timer,设置一个时间间隔。

表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1

target:(id)aTarget

表示发送的对象,如self

selector:(SEL)aSelector

方法选择器,在时间间隔内,选择调用一个实例方法

userInfo:(id)userInfo

此参数可以为nil,当定时器失效时,由你指定的对象保留和释放该定时器。

repeats:(BOOL)yesOrNo

当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。

invocation:(NSInvocation *)invocation

启动 Timer

  • – fire

停止 Timer

  • – invalidate

Timer设置

  • – isValid
  • – fireDate
  • – setFireDate:
  • – timeInterval
  • – userInfo
 

NSTimeInterval类:是一个浮点数字,用来定义秒

例子:

iphone为我们提供了一个很强大得时间定时器 NSTimer

他可以完成任何定时功能:

我们使用起来也很简单,只要记住三要素就可以,具体得三要素是:时间间隔NSTimeInterval浮点型,事件代理

delegate和事件处理方法@selector();就可以用

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; 来初始化一个 时间定时器

下面我写了一个很简单得例子

初始化一个定时器:

-(void)initTimer

{

//时间间隔

NSTimeInterval timeInterval =1.0 ;

//定时器

NSTimer   showTimer = [NSTimer scheduledTimerWithTimeInterval:maxShowTime

target:self

selector:@selector(handleMaxShowTimer:)

userInfo:nil

repeats:NO];

}

//触发事件

-(void)handleMaxShowTimer:(NSTimer *)theTimer

{

NSDateFormatter dateFormator = [[NSDateFormatter alloc] init];

dateFormator.dateFormat = @"yyyy-MM-dd  HH:mm:ss";

NSString *date = [dateformater stringFromDate:[NSDate date]];

if([date isEqualToString:@"2011-11-09 23:59:59"])

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TITLE_NAME

message:@"现在马上就有新的一天了!"

delegate:self

ancelButtonTitle:nil

otherButtonTitles:CONFIRM_TITLE, nil];

[alert show];

[alert release];

}

[data release];

[dateFormator release];

【编程技巧】NSTimer类的使用的更多相关文章

  1. java命名规范和编程技巧

    一个好的java程序首先命名要规范. 命名规范 定义这个规范的目的是让项目中所有的文档都看起来像一个人写的,增加可读性,方便维护等作用 Package 的命名 Package 的名字应该都是由一个小写 ...

  2. 深入理解C#:编程技巧总结(二)

    原创文章,转载请注明出处! 以下总结参阅了:MSDN文档.<C#高级编程>.<C#本质论>.前辈们的博客等资料,如有不正确的地方,请帮忙及时指出!以免误导! 在上一篇 深入理解 ...

  3. 15个提高编程技巧的JavaScript工具

    原文地址:http://www.imooc.com/wenda/detail/243523 JavaScript脚本库是一个预先用JavaScript语言写好的库,它方便了我们开发基于JavaScri ...

  4. WCF技术剖析之三十:一个很有用的WCF调用编程技巧[下篇]

    原文:WCF技术剖析之三十:一个很有用的WCF调用编程技巧[下篇] 在<上篇>中,我通过使用Delegate的方式解决了服务调用过程中的异常处理以及对服务代理的关闭.对于<WCF技术 ...

  5. WCF技术剖析之三十:一个很有用的WCF调用编程技巧[上篇]

    原文:WCF技术剖析之三十:一个很有用的WCF调用编程技巧[上篇] 在进行基于会话信道的WCF服务调用中,由于受到并发信道数量的限制,我们需要及时的关闭信道:当遇到某些异常,我们需要强行中止(Abor ...

  6. Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)

    原文:[原创]Matlab.NET混合编程技巧之--直接调用Matlab内置函数(附源码) 在我的上一篇文章[原创]Matlab.NET混编技巧之——找出Matlab内置函数中,已经大概的介绍了mat ...

  7. Matlab.NET混合编程技巧之——找出Matlab内置函数

    原文:[原创]Matlab.NET混合编程技巧之--找出Matlab内置函数 Matlab与.NET的混合编程,掌握了基本过程,加上一定的开发经验和算法基础,肯定不难.反之,有时候一个小错误,可能抓破 ...

  8. 18个Python高效编程技巧,Mark!

    初识Python语言,觉得python满足了我上学时候对编程语言的所有要求.python语言的高效编程技巧让我们这些大学曾经苦逼学了四年c或者c++的人,兴奋的不行不行的,终于解脱了.高级语言,如果做 ...

  9. STL中实现 iterator trail 的编程技巧

    STL中实现 iterator trail 的编程技巧 <泛型编程和 STL>笔记及思考. 这篇文章主要记录在 STL 中迭代器设计过程中出现的编程技巧,围绕的 STL 主题为 (迭代器特 ...

  10. 关于ThinkPHP的一些编程技巧

    在TP学习过程中难免会遇到一些大大小小的问题,把这些问题积累下来就可以在以后遇到时能很快速的解决,提高编程效率. 1.让Runtime下的文件格式化:入口文件处:define(‘STRIP_RUNTI ...

随机推荐

  1. 矩阵[雅礼集训 2017 Day1]

    SOL 奇奇怪怪的贪心(你也不要问我为什么) #include<bits/stdc++.h> #define N 1007 int f[N][N],sum,ans,Ha,bo,n,Ans; ...

  2. SEO之网站关键词的优化 :首页,内页关键字,长尾关键字

    这篇文章主要讲的是SEO之网站关键词的优化 :首页,内页关键字,长尾关键字. 为了查找方便,小A汇总了所有SEO优化的相关教程,方便大家查找到自己想要的SEO优化技巧: SEO优化教程汇总. 网站关键 ...

  3. JS通过decodeURIComponent函数解码

    在我们调用后台接口的时候,如果后端传过来的字段是编码过的,那需要使用decodeURIComponent函数进行解码显示 var test1="http://www.jianshu.com/ ...

  4. linux下一键安装redis并设置为后台进程及开机启动

    1.下载适合你的版本的redis(下载页面https://redis.io/download),我下载的是4.0.6版本 wget http://download.redis.io/releases/ ...

  5. mysql安装及常见使用

    mysql的安装和使用 说明:mysql是一个多线程,多用户的sql数据库,有着高性能,高可靠性,易于实用性等特点. 安装的软件链接:https://pan.baidu.com/s/1smRLkoX ...

  6. linux下安装ffmpeg

    1. 首先安装系统编译环境  yum install -y automake autoconf libtool gcc gcc-c++  #CentOS 2. 编译所需源码包 #yasm:汇编器,新版 ...

  7. JAVA进阶--ThreadPoolExecutor机制

    ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程 ...

  8. mybatis注解开发,动态sql

    在利用mybatis注解开始时,如果没有用到动态sql时,可以直接写 @Select("select * from order") List<XlSubOrder> g ...

  9. IdentityServer(11)- 使用Hybrid Flow并添加API访问控制

    关于Hybrid Flow 和 implicit flow 我在前一篇文章使用OpenID Connect添加用户认证中提到了implicit flow,那么它们是什么呢,它和Hybrid Flow有 ...

  10. cs231n spring 2017 lecture8 Deep Learning Networks 听课笔记

    1. CPU vs. GPU: CPU核心少(几个),更擅长串行任务.GPU有很多核心(几千个),每一个核都弱,有自己的内存(几个G),很适合并行任务.GPU最典型的应用是矩阵运算. GPU编程:1) ...