如何立即触发NSTimer?
我需要调用一个方法,当我的应用程序启动,然后调用每5秒相同的方法。
我的代码很简单:
// Call the method right away [self updatestuff:nil] // Set up a timer so the method is called every 5 seconds timer = [NSTimer scheduledTimerWithTimeInterval: 5 target: self selector: @selector(updatestuff:) userInfo: nil repeats: YES];
有定时器的选项,所以它是立即触发,然后每隔5秒?
NSTimer
的-fire
, [timer fire];
是你在找什么。
这将启动/立即调用计时器解除时间延迟。
您不能安排计时器,并立即在同一行代码中触发事件。 所以你必须在两行代码中完成。
首先,安排定时器,然后立即在定时器上调用'fire':
timer = [NSTimer scheduledTimerWithTimeInterval: 5 target: self selector: @selector(updatestuff:) userInfo: nil repeats: YES]; [timer fire];
当火焰被调用时,定时器等待触发的时间间隔将重置,因为它刚刚运行,然后在指定的时间间隔后继续运行 – 在这种情况下,每5秒钟一次。
您可以初始化计时器并在同一行中启动它:
[(checkStatusTimer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(checkStatusTimerTick:) userInfo:nil repeats:YES]) fire];
- iOS – 结合NSLineBreakByWordWrapping和NSLineBreakByTruncatingTail
- 在ios应用程序中共享语音备忘录文件
- iOS 9自定义转换 – animationControllerForDismissedController不被调用
- 我在UItextfield中input时如何更改我的UIlabel? 与旋转图标
- 无论如何缩短,如果(i == x || i == y)?
- 在Mac 10.5.8上开发iOS应用程序
- 为CGAffineTransformMakeRotation Swift设置旋转点
- Xcode 5文档窗口缺少sdk指南和示例的完整概述
- 应用程序:didReceiveRemoteNotification:fetchCompletionHandler:调用时不在UIApplicateStateBackground中