调用一次计时器方法:

//不重复,只调用一次。timer运行一次就会自动停止运行

self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self

selector: @selector(LocationTimer) userInfo:nil repeats:NO];

重复调用计时器方法:

//每1秒运行一次LocationTimer方法

self.locationTimer = [NSTimer scheduledTimerWithTimeInterval: target:self

selector: @selector(LocationTimer) userInfo:nil repeats:YES];

注意:将计数器的repeats设置为YES的时候,self的引用计数会加1。因此可能会导致self(即viewController)不能release,所以,必须在viewWillAppear的时候,将计数器timer停止,否则可能会导致内存泄露。

停止timer的运行,但这个是永久的停止:

//取消定时器

[self.locationTimer invalidate];

要想实现:先停止,然后再某种情况下再次开启运行timer,可以使用下面的方法:

首先关闭定时器不能使用上面的方法,应该使用下面的方法:

//关闭定时器

[self.locationTimer  setFireDate:[NSDate distantFuture]];

然后就可以使用下面的方法再此开启这个timer了:

//开启定时器

[self.locationTimer  setFireDate:[NSDate distantPast]];

例子:比如,在页面消失的时候关闭定时器,然后等页面再次打开的时候,又开启定时器。

(主要是为了防止它在后台运行,暂用CPU)可以使用下面的代码实现:

//页面将要进入前台,开启定时器

-(void)viewWillAppear:(BOOL)animated

{

//开启定时器

[self.locationTimer setFireDate:[NSDate distantPast]];

}

//页面消失,进入后台不显示该页面,关闭定时器

-(void)viewDidDisappear:(BOOL)animated

{

//关闭定时器

[self.locationTimerr setFireDate:[NSDate distantFuture]];

}

OK,搞定。 

版权声明:欢迎转载,请注明出处:蜗牛爱课 • 移动互联网

蜗牛爱课- iOS中定时器NSTimer使用的更多相关文章

  1. 蜗牛爱课 -- iOS 设计模式之模板模式

    1 前言 模板方法模式是面向对象软件设计中一种非常简单的设计模式.其基本思想是在抽象类的一个方法定义“标准”算法.在这个方法中调用的基本操作由子类重载予以实现.这个方法成为“模板”.因为方法定义的算法 ...

  2. IOS中定时器NSTimer的开启与关闭

    调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...

  3. 【转】iOS中定时器NSTimer的使用

    原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeI ...

  4. iOS中定时器NSTimer的使用-备用

    1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...

  5. 【转】IOS中定时器NSTimer的开启与关闭

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

  6. 【转】 IOS中定时器NSTimer的开启与关闭

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

  7. iOS中定时器NSTimer的使用/开启与关闭

      一.只调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5  ...

  8. ios 中定时器:NSTimer, CADisplayLink, GCD

    #import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...

  9. 蜗牛爱课 -- iOS 设置UIButton的字体的大小、显示位置、大小

    /设置按钮上的自体的大小 //[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法 ...

随机推荐

  1. mysql 5.5源码包安装

    注:由于mysql5.5的源码包安装与mysql之前的版本安装方法不同,故写一篇随笔记录.5.5的版本不再是./configure make make install 这里用到了cmake了,cmak ...

  2. office 文件在网页中显示

    1.如何在网页上显示word和excel a.可以使用office组件或aspose将word 和excel 转换为pdf 然后在网页上打开pdf,但是效果不是很好 .比如说excel 多个工作薄不是 ...

  3. C# winfrom 模拟ftp文件管理

    从网上找到的非常好用的模拟ftp管理代码,整理了一下,希望对需要的人有帮助 using System; using System.Collections.Generic; using System.T ...

  4. NPOI新建和读取EXCEL

    //基本NPOI 1.2.5.0 static void Main(string[] args) { string path = string.Format("E:\\export{0}.x ...

  5. ASP.NET在实际开发中验证码的用法

    在网上有看到很多关于验证码的代码,很多都只是生成一张验证码图片,然而在实际登陆验证模块,验证码要怎么添加进去或者说怎么运用.和实际项目开发中要怎么使用验证码,我自己总结了几点. 一.在实际开发登陆模块 ...

  6. js数字验证

    1.JS判断只能是数字和小数点 1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g, ...

  7. SDK编程模板

    #include<Windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI WinMain(HINS ...

  8. inux中tail命令---用于查看文件内容

    linux中tail命令---用于查看文件内容 最基本的是cat.more和less.1. 如果你只想看文件的前5行,可以使用head命令,如:head -5 /etc/passwd2. 如果你想查看 ...

  9. SharedPreferences基础

    见归档项目:SharedPreferencesDemo.zip 1.对于数据量较小,且有明显的K-V形式的数据而言,适合用SharedPreferences保存.SharedPreferences的数 ...

  10. [汇编语言]-debug跟踪执行

    ffff:0-ffff:d内存中数值求和放入dx寄存器中 代码: add.asm assume cs:code code segment mov ax,0ffffH mov ds,ax mov dx, ...