创建一个 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. 阻止form空表单提交----JavaScript

    网上看到很不错的阻止form空表单提交 第一种方法 <div class="warp"> <h2>登录到pfan空间</h2> <p> ...

  2. angular4.0 路由守卫详解

    在企业应用中权限.复杂页多路由数据处理.进入与离开路由数据处理这些是非常常见的需求. 当希望用户离开一个正常编辑页时,要中断并提醒用户是否真的要离开时,如果在Angular中应该怎么做呢? 其实Ang ...

  3. JavaScript Html页面加载完成

    //一.Html页面加载完成的JS写法 //1. $(function () {     alert("窗体Html页面加载完成方法一"); }); //2. $(document ...

  4. Linux 监测命令

    1.  ps  -ef -e显示所有进程:-f 显示完整格式的输出: 2.  ps  -l -l 显示一个长列表 3.  ps  -efH -H 用层级格式显示进程(树状) [ps 命令:显示某个特定 ...

  5. Promise实现小球的运动

        Promise简要说明 Promise可以处理一些异步操作:如像setTimeout.ajax处理异步操作是一函数回调的方式;当然ajax在jQuery版本升级过程中,编写方式也有所变动. P ...

  6. 到底啥事w3c标准

    W3C标准                                                                                               ...

  7. 简陋的swift carthage copy-frameworks 辅助脚本

    在看 carthage 时,其中需要配置一个 copy-frameworks 脚本,此脚本特殊的地方,需要手动填写 Input Files 和 Output Files.一个一个填写,很糟心~ 观察了 ...

  8. zz:linux下rz,sz的安装方法

    zz:http://xukaizijian.blog.163.com/blog/static/1704331192011611104631875/ wget http://ohse.de/uwe/re ...

  9. 安装配置Oracle 12c RAC

    http://www.lab128.com/rac12_installation_using_vb/article_text.html

  10. App开发 对生命周期的处理

    //获取到当前所在的视图 - (UIViewController *)presentingVC:(UIApplication *)application{ UIWindow * window = ap ...