Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:25:12+00:00 2026-05-30T14:25:12+00:00

This is my code for my stopwatch. It all works except for the lap

  • 0

This is my code for my stopwatch. It all works except for the lap button function. How would i be able to implement a lap time where when the ib action “lap” is pressed it would store the current time in an array and list the lap times on the view?

I have already tried to create a database but this seemed far to complex for something of this nature.

#import "FirstViewController.h"

@implementation FirstViewController
@synthesize start;
@synthesize stop;
@synthesize lap;
@synthesize reset;
@synthesize lapLabel;
@synthesize stopWatchLabel;

NSDate *startDate;
NSDateFormatter *dateFormatter;
NSTimer *stopWatchTimer;
NSTimeInterval secondsAlreadyRun;


int touchCount;



-(void)showActivity:(NSTimer *)tim {

    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    // Add the saved interval
    timeInterval += secondsAlreadyRun;
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss.SS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    stopWatchLabel.text = timeString;

}

- (IBAction)onStartPressed:(UIButton *)sender {

    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1/10 
                                                      target:self 
                                                    selector:@selector(showActivity:) 
                                                    userInfo:nil 
                                                     repeats:YES];
    // Save the new start date every time
    startDate = [[NSDate alloc] init]; // equivalent to [[NSDate date] retain];
    [stopWatchTimer fire];

    touchCount +=1;
    if (touchCount == 1) 
    {
        start.hidden = YES;
        stop.hidden = NO;
        reset.hidden = YES;
        lap.hidden = NO;
        touchCount = 0;
    }

}

- (IBAction)onStopPressed:(UIButton *)sender {
    // _Increment_ secondsAlreadyRun to allow for multiple pauses and restarts
    secondsAlreadyRun += fabs([startDate timeIntervalSinceNow]);
    [stopWatchTimer invalidate];
    stopWatchTimer = nil;
    stop.hidden = YES;
    start.hidden = NO;
    reset.hidden = NO;
    lap.hidden = YES;

}

- (IBAction)reset:(UIButton *)sender; {
    secondsAlreadyRun = 0;
    stopWatchLabel.text = @"00:00.00";
}

- (IBAction)lap:(UIButton *)sender; {
    //Lap Code will go here.

}

- (void)viewDidUnload {
    [self setStart:nil];
    [self setStop:nil];
    [self setLap:nil];
    [self setReset:nil];
    [self setLap:nil];
    [super viewDidUnload];
}
@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:25:14+00:00Added an answer on May 30, 2026 at 2:25 pm

    Just use a NSMutableArray stored in the class. Every time the person clicks the lap-button
    For instance call

    NSMutableArray *lapTimes;
    

    And in your method do:

    - (IBAction)lap:(UIButton *)sender {
        double timeSinceStart = [startDate timeIntervalSinceNow];
        [lapTimes addObject:[NSNumber numberWithDouble:-timeSinceStart]];
    }
    

    timeIntervalSinceNow will return the difference in time between the NSDate object and now in seconds. If the NSDate is earlier then now (like in your case), the returned number will be negative, so you will have to invert it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
I've been looking at this code: http://www.jquery4u.com/jquery-date-and-time-2/online-jquery-stopwatch/ I'm trying to understand exactly how it
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code is executed by many way. When it's executed by the form button
This code function LoadContent(Id) { alert('Controls/Network/NetworkDetail.aspx?' + rnd() + '&CtlId=' + Id); $.get('Controls/Network/NetworkDetail.aspx?' +
This is my code for an iPhone stopwatch app, when you hit the start
I have the this code: var options = GetOptions(From, Value, SelectedValue); var stopWatch =
I have this code to list all the files in a directory. class GetTypesProfiler
I have the following code for a stopwatch function. I have 2 UIButtons. One
i just tried multiple routing via code with the mappoint api. It works all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.