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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:02:03+00:00 2026-05-29T15:02:03+00:00

I have a timer class set up that is basically handling all of count

  • 0

I have a timer class set up that is basically handling all of count down timer logic. All it does is on button press – counts from 60 to 0 seconds.

I have the following code in a Timer.m class.

- (void)advanceTimer
{
    self.lengthOfTime = [NSNumber numberWithInt:self.lengthOfTime.intValue - 1];
    NSLog(@"%@",self.lengthOfTime);
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(advanceTimer) userInfo:nil repeats:NO];
}
- (void)startCountdown
{
    if (!self.lengthOfTime) self.lengthOfTime = [NSNumber numberWithInt:60];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(advanceTimer) userInfo:nil repeats:NO];
}

What I am looking to do is create a timer object in my View Controller that will update a label from the ViewController.m. Currently – the class works because I can NSLog from the Timer class and it counts down correctly. I thought about having the advanceTimer method return – but I can’t seem to wrap my head around how to update the label in the ViewController with the returned data.

The only way I cold get the return to work was to have a button that refreshed the label to the correct countdown time… I can’t get it to automatically count down…

  • 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-29T15:02:05+00:00Added an answer on May 29, 2026 at 3:02 pm

    Well, if you know how to update a label by clicking a button, you have everything in place to connect everything else:

    If your view controller has an IBOutlet for the label and an IBAction that updates it, why not call the view-controller’s action in your advanceTimer method?

    Yet easier, you could connect your timer class to the label.
    You might do it like this:

    // Timer.h:
    @interface Timer : NSObject
    
    @property (retain, nonatomic) IBOutlet UILabel *timeLabel;
    @property (assign, nonatomic) NSInteger secondsRemaining;
    @property (assign, nonatomic) NSTimer *timer;
    
    - (IBAction)startCountdown:(id)sender;
    - (IBAction)stopCountdown:(id)sender;
    - (void)timerFired:(NSTimer *)timer;
    
    @end
    
    // Timer.m
    @implementation Timer
    
    @synthesize timeLabel = timeLabel_;
    @synthesize secondsRemaining = secondsRemaining;
    @synthesize timer = timer_;
    
    - (void)setTimer:(NSTimer *)timer
    {
        if (timer = timer_)
            return;
    
        [timer_ invalidate];
        timer_ = timer;
    }
    
    - (void)scheduleTimer
    {
        if (self.secondsRemaining <= 0) {
            self.timer = nil;
        } else {
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
        }
    }
    
    - (void)timerFired:(NSTimer *)timer
    {
        self.secondsRemaining -= 1;
        NSString *displayString = [NSString stringWithFormat:@"%d", self.secondsRemaining];
        self.timeLabel.text = displayString;
        [self scheduleTimer];
    }
    
    - (IBAction)startCountdown:(id)sender
    {
        self.secondsRemaining = 60;
        [self scheduleTimer];
    }
    
    - (IBAction)stopCountdown:(id)sender
    {
        self.timer = nil;
    }
    
    - (void)dealloc
    {
        [timeLabel_ release];
        [super dealloc];
    }
    
    @end
    

    This code has a two benefits:

    • You can cancel your timer.
    • Your view controller does not need to know anything about this — you can set this up in interface builder, entirely.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that will be accessed from two threads: class Timer{ public:
I have several classes that are basically interfaces to database rows. Since the class
Basically I have run-time loaded class that looks like this: [Plugin(Plugin name)] class PluginActions
I have a static timer class which will be called by ANY webpage to
I have an HTTP Request Dispatcher class that works most of the time, but
I have a Products:List<Product> class. I'd like to make it so that every time
I have a some code that gets passed a class derived from a certain
I have defined a C#-class, that shall be the elements of a directed graph
I have a class, the outline of which is basically listed below. import org.apache.commons.math.stat.Frequency;
have a really annoying time developing some code for a set of UserControls that

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.