iOS,WKInterfaceTimer启动计时器

kat*_*rin 5 objective-c nsdate nstimer ios watchkit

我正在使用WatchKit开发一个应用程序

我想创建一个WKInterfaceTimer,当我点击一个按钮时,计时器以0秒开始

但是当我运行应用程序时,计时器自动启动,我无法在之前或之后停止计时器

这是代码:

 [self.mytimer setHidden:NO];
 [self.mytimer start];
Run Code Online (Sandbox Code Playgroud)

在故事板中,我设置了短暂的"第二分钟小时",所有内容都勾选并启用了未启用

我需要使用吗? [self.mytimer setDate:];

如果是,请给我一个确切的短字符串,显示标签0 secends

Sie*_*ike 7

我不确定你的问题,但我在我的故事板中放了一个带有按钮的计时器.然后我确保使用缩写格式启用计时器(确保选择了秒,分钟和小时).确保计时器已启用,否则它将不会出现在您的视图中,目前我找不到以编程方式启用计时器的方法.还要确保在按下开始按钮时设置日期,否则计时器将在不知道后台的情况下开始计时.在WKInterfaceTimer的类中,start方法仅更新标签,实际上并不启动任何类型的计时器.下面的代码使计时器从0开始计数.

#import "InterfaceController.h"


@interface InterfaceController()
@property (weak, nonatomic) IBOutlet WKInterfaceTimer *testTimer;
@end


@implementation InterfaceController

- (instancetype) initWithContext: (id) context
{
    self = [super initWithContext:context];
    if (self)
    {
        NSLog(@"Custom init called for InterfaceController!");
    }
    return self;
}

- (void) willActivate
{
    [self.testTimer stop];
    NSLog(@"Activated!");
}

- (IBAction) onStartButtonPressed
{
    [self.testTimer setDate:[NSDate dateWithTimeIntervalSinceNow:-1]];
    [self.testTimer start];
    NSLog(@"Start button pressed!");
}
@end
Run Code Online (Sandbox Code Playgroud)

在willActivate下停止计时器的原因是,一旦显示视图,它就不会开始向上计数.

  • 我有个问题.[NSDate dateWithTimeIntervalSinceNow:-1],为什么"-1"? (2认同)