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

  • Home
  • SEARCH
  • 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 8436247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:07:49+00:00 2026-06-10T07:07:49+00:00

This question has been asked to death on stack overflow, and I’ve seen a

  • 0

This question has been asked to death on stack overflow, and I’ve seen a plethora of answers but somehow I’m still having trouble.

Anyway, I allocate an NSTimer as so in my view did load:

NSTimer *oneSecondTicker = [[NSTimer alloc] init]; 
oneSecondTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabelsWithOneSecondTicker) userInfo:nil repeats:YES]; 
self.dateForTimeLabel = [NSDate date];

the method that is the selector is:

-(void) updateLabelsWithOneSecondTicker {
    if(self.dateForTimeLabel != nil)  
    {
        self.lblTime.text = [NSString stringWithFormat:@"%f", fabs([self.dateForTimeLabel timeIntervalSinceNow])];
    }
}

This method basically updates a label each second, giving me a timer/stopwatch sort of thing.

I also have a start/pause button, which when pressed to pause does:

[oneSecondTicker invalidate];
oneSecondTicker = nil;

if the button is pressed to start again, the method is:

NSTimer *oneSecondTicker = [[NSTimer alloc] init];
oneSecondTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabelsWithOneSecondTicker) userInfo:nil repeats:YES];    

self.dateForTimeLabel = [NSDate date];

I do not call retain to the timer, and it has no property and it does not get synthesized. It is however getting declared in the interface.

To get to the issue, what happens with each pause/start button press is that the timers do not get invalidated, and the time label becomes updated quicker and quicker, leading me to believe that multiple timers exist for some reason. (Obviously the functionality for a real timer is not to be found in these methods yet, and this is just a test to get my NSTimer to work).

How can this be?

  • 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-10T07:07:50+00:00Added an answer on June 10, 2026 at 7:07 am

    You wrote this:

    NSTimer *oneSecondTicker = [[NSTimer alloc] init];
    

    That line creates a local variable. It does not set the property that you declared in your interface. That line also creates a timer, which you immediately destroy in the next line when you reassign oneSecondTicker to point at the timer you create with scheduledTimerWithTimeInterval:. This is a beginner mistake that indicates that you need to learn how pointers work.

    Anyway, you’re creating your timer using scheduledTimerWithTimeInterval:.... That means the timer is automatically scheduling itself in the run loop. When a timer is scheduled in a run loop, the run loop retains the timer. That’s why (assuming you’re using ARC) the timer lives on even though you’re releasing your reference to it.

    You need to store the timer in your instance variable. I assume you are using Xcode 4.4 or later, so the property is being automatically synthesized. If you declared the property like @property (nonatomic, strong) NSTimer *oneSecondTicker, then you can create the timer like this:

    self.oneSecondTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabelsWithOneSecondTicker) userInfo:nil repeats:YES]; 
    

    and when you want to invaldate it, you can say this:

    [self.oneSecondTicker invalidate];
    self.oneSecondTicker = nil;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been asked before but i still don't understand it fully so
This question has been asked before but the answers aren't always clear or are
This question has been asked a lot but everywhere the answers fall short. I
This question has been asked before and there have been windows-specific answers but no
I know this question has been asked to death here, but I am unable
This question has been asked before but I still cannot figure whats wrong for
I know this question has been asked to death but nothing through searching has
I realise that this question has probably been asked to death, but none of
This question has been asked in different ways before, but the answers don't help
I'd assume this question has been asked to death, but I'm not finding anything

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.