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 9054835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:47:12+00:00 2026-06-16T13:47:12+00:00

Here is my situation: I am making a countdown app which works fine but

  • 0

Here is my situation: I am making a countdown app which works fine but does not appear to stop when I call [stopWatchTimer invalidate];, and I have no idea why. Here is my code:

- (IBAction)btnStartPressed:(id)sender {
//Start countdown with the time on the Date Picker.

    timeLeft = [pkrTime countDownDuration];

    [self currentCount];

    lblTimer.text = time; //sets the label to the time set above

    pkrTime.hidden = YES;
    btnStart.hidden = YES;
    btnStop.hidden = NO;

    //Fire this timer every second.
    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/1.0
                                                  target:self
                                                selector:@selector(reduceTimeLeft:)
                                                userInfo:nil
                                                 repeats:YES];
}
- (void)reduceTimeLeft:(NSTimer *)timer {
    //Countown timeleft by a second each time this function is called
    timeLeft--;
    // Get the system calendar
    NSCalendar *sysCalendar = [NSCalendar currentCalendar];

    // Create the NSDates
    NSDate *date1 = [[NSDate alloc] init];
    NSDate *date2 = [[NSDate alloc] initWithTimeInterval:timeLeft sinceDate:date1];

    // Get conversion to months, days, hours, minutes
    unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

    NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1  toDate:date2  options:0];

    int sec = [conversionInfo second];
    int min = [conversionInfo minute];
    int hour = [conversionInfo hour];

    NSString *seconds = [NSString stringWithFormat:@"%d",sec];
    NSString *minutes = [NSString stringWithFormat:@"%d",min];

    if (sec <= 9)
        seconds = [NSString stringWithFormat:@"0%d", sec];
    if (min <= 9)
        minutes = [NSString stringWithFormat:@"0%d", min];

    if ([conversionInfo hour] == 0)
        time = [NSString stringWithFormat:@"%@:%@", minutes, seconds];
    else
        time = [NSString stringWithFormat:@"%d:%@:%@", hour, minutes, seconds];

    lblTimer.text = time; //sets the label to the time set above

    NSLog(@"%d", timeLeft);

    if (timeLeft == 0) {
        [self timerDone];
        [stopWatchTimer invalidate];
        stopWatchTimer = nil;
    }
}

-(void)timerDone {

    pkrTime.hidden = NO;
    btnStart.hidden = NO;
    btnStop.hidden = YES;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Timer Done" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
    [alert show];

    [self playAlert];
}

Please let me know what the problem is… I cannot find a problem with my code anywhere!

  • 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-06-16T13:47:13+00:00Added an answer on June 16, 2026 at 1:47 pm

    In your btnStartPressed: method, you have nothing preventing a second NSTimer from being allocated and assigned to stopWatchTimer. If you press the button twice, you’ll end up with two timers, but only one will ever be invalidated.

    Add something like:

     if (stopWatchTimer) return;
    

    To the beginning of btnStartPressed:. If that doesn’t fix the problem, then there isn’t enough context to know for sure what is going on beyond conjecturing that timeLeft is zero?


    What Nate said, but here is another explanation.

    Imagine if you do this (where stopWatchTimer is a global or instance variable, doesn’t matter):

     stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:....];
    

    Now, do this:

     stopWatchTimer = nil;
     [stopWatchTimer invalidate];
    

    The timer won’t invalidate, but it’ll still fire. stopWatchTimer is a reference to the object. It isn’t the object itself. Thus, when you assign a second timer to stopWatchTimer, you are overwriting the reference to the first timer, but that timer is still going to fire!

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

Sidebar

Related Questions

Here is the situation. I am making changes to an application but I do
Here's my situation: I am making synchronous HTTP requests to collect data but before
Here is the situation: I am making a small prog to parse server log
Ok, so here's the situation. I'm making a program where you can save and
I have a strange situation here. In my android app, I have a layout
Now here is my situation: I'm making a CMS. When links are clicked, i
Here's the situation: I am making a multiplayer version of pong for android. When
The question title is rather vague, but here's my situation. I have roughly 700+
Let me first start with explaining the situation. I am making a IOS app
This is often situation, but here is latest example: Companies have various contact data

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.