如何使用NSTimer? [英] How do I use NSTimer?

查看:90
本文介绍了如何使用NSTimer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 NSTimer

解决方案

首先,我想提请你注意Cocoa / CF文档这总是一个伟大的第一个呼叫端口)。 Apple文档在每篇参考文章顶部都有一个名为Companion Guides的部分,其中列出了正在记录的主题(如果存在)的指南。例如,使用 NSTimer 文档列出了两个配套指南:





文章可能是最有用的,而线程主题是相关的,但不是与被记录的类最直接相关。如果你看看定时器编程主题文章,它分为两部分:




  • 定时器

  • 使用计时器



对于采用此格式的文章,通常会对类及其使用情况进行概述,然后一些示例代码在如何使用它,在这种情况下在使用定时器部分。有创建和调度定时器,停止定时器和内存管理部分。从文章中,创建一个计划的,不重复的计时器可以这样做:

  [NSTimer scheduledTimerWithTimeInterval:2.0 
target:self
selector:@selector(targetMethod :)
userInfo:nil
repeats:NO];

这将创建一个计时器,在2.0秒后触发,并调用 targetMethod: c> 上的自带一个参数,它是指向 NSTimer 实例的指针。 p>

如果您想在方法中查看更多细节,您可以参考文档获取更多信息,但也有代码的解释。



如果你想停止一个重复的计时器(或者在它被触发之前停止非重复计时器),那么你需要保留一个指向 NSTimer 创建的实例;通常这将需要是一个实例变量,以便您可以在另一个方法中引用它。然后,您可以在 NSTimer 实例上调用 invalidate

  [myTimer invalidate]; 
myTimer = nil;

这也是一个好习惯, nil 实例变量(例如,如果您的使计时器无效的方法被多次调用,并且实例变量未设置为 nil NSTimer 实例已被释放,它将抛出异常。)



请注意文章底部的内存管理: p>


因为运行循环维护了定时器,从内存管理的角度来看,通常不需要保存一个定时器的引用已计划。由于当您将方法指定为选择器时,计时器会作为参数传递,因此您可以在该方法中适当时使重复计时器无效。然而,在许多情况下,您还需要使定时器无效的选项 - 甚至在开始之前。 在这种情况下,您需要保留对计时器的引用,以便在适当时可以向其发送无效消息。如果创建了非计划的计时器(参见非计划的计时器),则必须保持对计时器的强引用(在引用计数的环境中,您保留它),以便在使用计时器之前不会释放它。



How do I use an NSTimer? Can anyone give me step by step instructions?

解决方案

Firstly I'd like to draw your attention to the Cocoa/CF documentation (which is always a great first port of call). The Apple docs have a section at the top of each reference article called "Companion Guides", which lists guides for the topic being documented (if any exist). For example, with NSTimer, the documentation lists two companion guides:

For your situation, the Timer Programming Topics article is likely to be the most useful, whilst threading topics are related but not the most directly related to the class being documented. If you take a look at the Timer Programming Topics article, it's divided into two parts:

  • Timers
  • Using Timers

For articles that take this format, there is often an overview of the class and what it's used for, and then some sample code on how to use it, in this case in the "Using Timers" section. There are sections on "Creating and Scheduling a Timer", "Stopping a Timer" and "Memory Management". From the article, creating a scheduled, non-repeating timer can be done something like this:

[NSTimer scheduledTimerWithTimeInterval:2.0
    target:self
    selector:@selector(targetMethod:)
    userInfo:nil
    repeats:NO];

This will create a timer that is fired after 2.0 seconds and calls targetMethod: on self with one argument, which is a pointer to the NSTimer instance.

If you then want to look in more detail at the method you can refer back to the docs for more information, but there is explanation around the code too.

If you want to stop a timer that is one which repeats, (or stop a non-repeating timer before it fires) then you need to keep a pointer to the NSTimer instance that was created; often this will need to be an instance variable so that you can refer to it in another method. You can then call invalidate on the NSTimer instance:

[myTimer invalidate];
myTimer = nil;

It's also good practice to nil out the instance variable (for example if your method that invalidates the timer is called more than once and the instance variable hasn't been set to nil and the NSTimer instance has been deallocated, it will throw an exception).

Note also the point on Memory Management at the bottom of the article:

Because the run loop maintains the timer, from the perspective of memory management there's typically no need to keep a reference to a timer after you’ve scheduled it. Since the timer is passed as an argument when you specify its method as a selector, you can invalidate a repeating timer when appropriate within that method. In many situations, however, you also want the option of invalidating the timer—perhaps even before it starts. In this case, you do need to keep a reference to the timer, so that you can send it an invalidate message whenever appropriate. If you create an unscheduled timer (see "Unscheduled Timers"), then you must maintain a strong reference to the timer (in a reference-counted environment, you retain it) so that it is not deallocated before you use it.

这篇关于如何使用NSTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆