Simple StopWatch in Cocoa, Objective C for Mac OSX

Simple Stop Watch in Cocoa, Objective C for Mac OSX

In this example, sample application we will create a simple StopWatch Mac OSX application.

The stopwatch will be 1/10 the second precision

The application will have the following

  • Start button – That starts the stopwatch
  • Stop button – Stops the timer/ stopwatch and on clicking start again will restart the stopwatch from the last state.
  • Reset button – Resets the stopwatch

The final output will look like as follows

KSStopwatch1KSStopwatch2

//
//  AppDelegate.h
//  StopWatch
//
//  Created by Debasis Das on 10/13/14.
//  Copyright (c) 2014 Knowstack. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject 
{

}
@property (nonatomic, retain) NSTimer *appTimer;
@property (nonatomic, assign) int timerCount;
@property (nonatomic, assign) IBOutlet NSTextField *stopWatchLabelTextField;
@end

//
//  AppDelegate.m
//  StopWatch
//
//  Created by Debasis Das on 10/13/14.
//  Copyright (c) 2014 Knowstack. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    self.timerCount = 0;
    [self.stopWatchLabelTextField setStringValue:[self getFormattedString]];
}

//This is the main method that formats the counter into a stopwatch format.
format(0) = 0:00.0
format(11) = 0:01.1
format(321) = 0:32.1
format(613) = 1:01.3

-(NSString *)getFormattedString
{
    int t = self.timerCount;
    int minQuotient = t / 600;
    int minRemainder = t % 600;
    int secQuotient = minRemainder / 10;
    int secRemainder = minRemainder % 10;
    
    NSString * minString = [NSString stringWithFormat:@"%d",minQuotient];
    NSString * secString = [NSString stringWithFormat:@"%d",secQuotient];
    
    if (secQuotient < 10){
        secString = [NSString stringWithFormat:@"0%@",secString];
    }
    NSString *secRemainderString = [NSString stringWithFormat:@"%d",secRemainder];
    
    return [NSString stringWithFormat:@"%@:%@.%@",minString,secString,secRemainderString];
}
     

-(IBAction)startStopWatch:(id)sender
{
    [self.appTimer invalidate];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                      target:self selector:@selector(stopWatch:)
                                                    userInfo:nil repeats:YES];
    self.appTimer = timer;
}

-(IBAction)stopStopWatch:(id)sender
{
    [self.appTimer invalidate];
}

-(IBAction)resetStopWatch:(id)sender
{
    [self.appTimer invalidate];
    self.timerCount = 0;
    [self.stopWatchLabelTextField setStringValue:[self getFormattedString]];
}

- (void)stopWatch:(NSTimer*)theTimer {
    self.timerCount = self.timerCount + 1;
    [self.stopWatchLabelTextField setStringValue:[self getFormattedString]];
    
}
@end

The source code can be found at the following location

https://github.com/knowstack/DDStopWatch.git

Created by: Debasis Das

Posted in Cocoa, Objective C Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news