IOS, WKInterfaceTimer Startup Timer

I am developing an application using WatchKit

I want to create one WKInterfaceTimer , when I press one button, the timer starts from 0 seconds

but when I start the application, the timer starts automatically, and I can not stop the timer before or after

here is the code:

[self.mytimer setHidden:NO]; [self.mytimer start]; 

In the storyboard I set a short β€œsecond minute hour”, all the checkmarks and β€œEnabled” are inactive

Do I need to use [self.mytimer setDate:];

If yes, please give me the exact short line with the label 0 secends

+5
source share
1 answer

I'm not quite sure what you are asking for, but I set a timer with a button under it in my storyboard for watches. Then I made sure that the timer is enabled with a shortened format (make sure that the second, minute and hour are selected). Make sure the timer is on, otherwise it will not appear in your view, and at present I could not find a way to enable the timer programmatically. Also make sure that you set the date when you pressed the start button, or the timer starts counting without knowing in the background. In the WKInterfaceTimer class, the start method only updates the label and does not actually start any timer. In the code below, the timer is counted from 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 

The reason the timer stops in the willActivate file is because it will not start counting up as soon as the view is displayed.

+7
source

Source: https://habr.com/ru/post/1208287/


All Articles