ios - 观看 OS2 NSTimer 问题

标签 ios xcode nstimer watchkit watchos-2

我正在开发一个应用程序,当 watch 被激活时,我需要启动一个计时器(使用 NSTimer)。使用计时器,我向 iPhone 询问一些信息(大约每 1 秒一次,最多 5 秒一次)。我用它来启动计时器

timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(myfunction) userInfo:nil repeats:NO];

在“myfunction”函数中,我下次重新启动定时器。

- (void) myfunction
{
   //Here I update a label text
   // [...]

   [timer invalidate];
   timer = nil;

   counter++;
   if(counter<5)
   {
     timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(myfunction) userInfo:nil repeats:NO];
   }
}

我的问题是,在模拟器中一切正常,但在真正的 watch (Watch-OS2 GM)中,计时器有时不启动或有时启动但只启动一次,之后似乎卡住了!我看到这个是因为我在显示计数器的每个耗时段更新 watch 中的标签,并且我确定所有内容都在“将激活”功能中初始化。我不明白为什么。有人有同样的问题吗?

最佳答案

来自 documentation

Use your interface controller’s init and awakeWithContext: methods to load any required data, set the values for any interface objects, and prepare your interface to be displayed. Do not use the willActivate to initialize your interface controller. The willActivate method is called shortly before your interface is displayed onscreen, so you should use that method only to make last-minute changes. For example, you might also use that method to start animations or start other tasks that should only happen while your interface is onscreen.

那么,你是用什么方法来实例化timer的呢?

确保使用willActivate 方法并使用didDeactivate 方法来清理界面并将其置于静止状态。例如,使用此方法使计时器失效并停止动画。

希望对你有帮助

关于ios - 观看 OS2 NSTimer 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32689086/

相关文章:

ios - 使用 IQKeyboardReturnKeyHandler 在键盘上方显示 uitextfield

ios - 如何删除 xcode 项目中的子模块方案?

c++ - 向 PDF 添加 FreeText 注释

ios - 无法在 iOS 10 中使用分布式配置文件验证企业应用程序

iphone - 应用程序在后台时计划的 NSTimer?

ios - 从 'String' 转换为不相关类型 'LocationModel' 始终失败警告

ios - PubNub使用,Swift 2.0更新 "Ambiguous use of '下标'"+ "排序》

swift - 如何将 UITableViewCell 的元素限制在导航栏的底部?

cocoa - 当 runloop 被阻塞时 NSTimer 不会触发

iphone - 应用程序中的多个 NSTimer