Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS , WKInterfaceTimer Start timer

I'm developing an app using WatchKit

I want to create one WKInterfaceTimer, when I tap in one button the timer starts with 0 Second

but when I run the application the timer automaticaly start and I'm not able to stop timer before or after

here is the code:

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

In the storyboard I have set short "second minutes hour" all ticked and Enabled unticked

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

If yes please give me the exact for short string with showing label 0 secends

like image 250
katrin Avatar asked Dec 03 '14 20:12

katrin


1 Answers

I'm not exactly sure what your asking but I placed a timer with a button below it in my storyboard for the watch. Then I made sure the timer was enabled with abbreviated format (Making sure second, minute, and hour was selected). Make sure the timer is enabled else it will not appear in your view and currently I could not find a way to enable the timer programmatically. Also make sure you set the date when you hit the start button or else the timer will start counting up without you knowing in the background. In WKInterfaceTimer's class, the start method only updates the label and does not actually start any kind of timer. The code below makes the timer count up 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 for stopping the timer under willActivate is so that it will not start counting upwards as soon as the view is displayed.

like image 107
SierraMike Avatar answered Nov 15 '22 00:11

SierraMike