将 NSTimer 设置为将来触发一次
如何设置 NSTimer 在将来触发一次(例如 30 秒)。到目前为止,我只能将其设置为立即触发,然后每隔一段时间触发。
How do I set up an NSTimer to fire once in the future (say, 30 seconds). So far, I have only managed to set it so it fires immediately, and then at intervals.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您要使用的方法是:
使用
repeats == NO
参数和seconds == 30
。这将创建计时器并安排它。它只会在 30 秒内触发一次(而不是立即触发)。The method you want to use is:
with
repeats == NO
arguments andseconds == 30
. This will create the timer and schedule it. It will fire only once, in 30 seconds (and not immediately).您可以根据未来的日期设置计时器,并将重复设置为“否”
You can set the timer with your future date, and set repeats to NO
使用此类方法来调度计时器。
参数
秒
计时器触发之间的秒数。如果秒小于或等于 0.0,则此方法会选择非负值 0.1 毫秒。
目标
当计时器触发时,要向其发送由 aSelector 指定的消息的对象。目标对象由定时器保留,并在定时器失效时释放。
选择器
计时器触发时发送到目标的消息。选择器必须具有以下签名:
- (void)timerFireMethod:(NSTimer*)theTimer
计时器将其自身作为参数传递给此方法。
用户信息
计时器的用户信息。您指定的对象由计时器保留,并在计时器失效时释放。该参数可能为零。
重复
如果是,计时器将重复重新调度自身,直到失效。如果为“否”,计时器将在触发后失效。
示例
计时器在 2 秒后由运行循环自动触发。 定时器编程主题
Use this class method to schedule timer.
Parameters
seconds
The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.
target
The object to which to send the message specified by aSelector when the timer fires. The target object is retained by the timer and released when the timer is invalidated.
aSelector
The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
The timer passes itself as the argument to this method.
userInfo
The user info for the timer. The object you specify is retained by the timer and released when the timer is invalidated. This parameter may be nil.
repeats
If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
Example
The timer is automatically fired by the run loop after 2 seconds. Timer Programming Topics