iOS之 NSTimer(二)

1. Stopping a Timer  关闭定时器

if you create a non-repeating timer, there is no need to take any further action. It automatically stops itself after it fires. For example, there is no need to stop the timer created in the Initializing a Timer with a Fire Date. If you create a repeating timer, however, you stop it by sending it an invalidate message. You can also send a non-repeating timer an invalidate message before it fires to prevent it from firing.

上面的就是说可以用timer的引用 直接 invalidate。

- (IBAction)stopRepeatingTimer:sender {
    [self.repeatingTimer invalidate];
    self.repeatingTimer = nil;
}

- (IBAction)stopUnregisteredTimer:sender {
    [self.unregisteredTimer invalidate];
    self.unregisteredTimer = nil;
}

You can also invalidate a timer from the method it invokes.

你也可以在它的调度方法中对timer 进行invalidate,  This will invalidate the timer after it has fired three times. Because the timer is passed as an argument to the method it invokes, there may be no need to maintain the timer as a variable.

- (void)countedTimerFireMethod:(NSTimer*)theTimer {

    NSDate *startDate = [[theTimer userInfo] objectForKey:@"StartDate"];
    NSLog(@"Timer started on %@; fire count %d", startDate, self.timerCount);

    self.timerCount++;
    if (self.timerCount > 3) {
        [theTimer invalidate];
    }
}

however, you might nevertheless keep a reference to the timer in case you want the option of stopping it earlier.

// The repeating timer is a weak property.
@property (weak) NSTimer *repeatingTimer;
@property (strong) NSTimer *unregisteredTimer;

保持对timer的引用,这样你可以在你可以 在你想要的地方stop(invilidate) 这个timer。

时间: 2024-09-05 04:12:46

iOS之 NSTimer(二)的相关文章

IOS系列——NStimer

Timer常用的一些东西 1. 初始化 timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTime:) userInfo:nil <span style="font-family: Arial, Helvetica, sans-serif;"> repeats:YES];</span> 2.timer 马上执行 [tiemr fire];

IOS 中NSTimer使用注意事项

1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelec

IOS设计模式之二(门面模式,装饰器模式)

本文原文请见:http://www.raywenderlich.com/46988/ios-design-patterns. 由 @krq_tiger(http://weibo.com/xmuzyq)翻译,如果你发现有什么错误,请与我联系谢谢. 门面(Facade)模式(译者注:facade有些书籍译为门面,有些书籍译为外观,此处译为门面) 门面模式针对复杂的子系统提供了单一的接口,不需要暴漏一些列的类和API给用户,你仅仅暴漏一个简单统一的API. 下面的图解释了这个概念: 这个API的使用者

零基础学习IOS开发(二)- 使用cocos2d-x3.0 执行Hello world

关于开发框架,依据网上检索来的信息,感觉cocos2d-x的ios游戏开发框架非常不错,并且有非常强的可移植性,因此打算尝试一下. 截止写下此文章,最新的cocos2d-x的版本号为v3.0稳定版(几天前刚刚公布),下载地址为:http://www.cocos2d-x.org/filedown/cocos2d-x-3.0-cn 安装方式例如以下: 1.解压程序压缩包(所在路径不能有中文哦,由于是python,你懂的...) 2.sudo python setup.py 安装cocos2d-x,依

学iOS开发(二)——实现一款App之编写自定义类

今天学iOS开发(二)--实现一款App之编写自定义类 当开发iOS应用程序时,你会发现在许多场景下,你需要编写自己的自定义类.当你需要数据和自定义行为一起打包时,自定义类就很有用了.在一个自定义的类中,你可以为存储.操纵和显示数据定义你自己的行为. 例如,考虑iOS Clock app中的全球时钟标签(World Clock tab).这个表视图中的单元格需要显示比标准表视图单元格更多的内容.这是一个不错的机会实现一个扩展UITableViewCell行为的子类,从而在给定的表视图单元格中显示

iOS事件机制(二)

iOS事件机制(二) DEC 29TH, 2013 本篇内容接上一篇iOS事件机制(一),本次主要介绍iOS事件中的多点触控事件和手势事件. 从上一篇的内容我们知道,在iOS中一个事件用一个UIEvent对象表示,UITouch用来表示一次对屏幕的操作动作,由多个UITouch对象构成了一个UIEvent对象.另外,UIResponder是所有响应者的父类,UIView.UIViewController.UIWindow.UIApplication都直接或间接的集成了UIResponder.关于

【HELLO MAKA】MAKA iOS客户端 之二 架构设计与实现篇

上一篇主要做了MAKA APP的需求分析,功能结构分解,架构分析,API分析,API数据结构分析. 这篇主要讲如何从零做iOS应用架构. [HELLO MAKA]MAKA iOS客户端 之一 APP分析篇 [HELLO MAKA]MAKA iOS客户端 之二 架构设计与实现篇 [HELLO MAKA]MAKA iOS客户端 之三 创作模块分析与实现篇 1.  iOS客户端架构 按照功能模块划分.这里可以使用二层设计也可以使用三层设计.MVC, MVCS, MVVM, MVP, VIPER, DD

iOS 中 NSTimer 使用详解-北京尚学堂

iOS 中 NSTimer 使用详解-北京尚学堂 前阵子在整理公司项目的时候,发现老代码在使用 NSTimer 时出现了内存泄露.然后整理了一些 NSTimer 的相关内容.比较简单,各位见笑啦. NSTimer fire 我们先用 NSTimer来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface DetailViewController () @property (nonatomic, weak) NSTimer *timer; @end

微信连WiFi关注公众号流程更新 解决ios微信扫描二维码不关注就能上网的问题

前几天鼓捣了一下微信连WiFi功能,设置还蛮简单的,但ytkah发现如果是ios版微信扫描微信连WiFi生成的二维码不用关注公众号就可以直接上网了,而安卓版需要关注公众号才能上网,这样就少了很多ios用户的关注用户,ios用户的线上消费水平普遍比安卓的高.现在好了,微信连WiFi上线连网后关注公众号的流程更新了,解决ios微信扫描二维码不关注就能上网的问题. 还在为无法转化到店顾客为粉丝而烦恼吗? 微信连WiFi上线连网后关注公众号的新流程,让顾客在连你的WiFi时感知你的品牌,成为你的粉丝.