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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:34:34+00:00 2026-06-17T08:34:34+00:00

I have a problem with my timer not running repeatedly. I’m still new to

  • 0

I have a problem with my timer not running repeatedly. I’m still new to coding for the iOS but from what I have gathered on this website and answers to other questions, it should work. Basically the timer should start updating when the timerStart button is pressed and it should stop when the timerStop button is pressed.

Heres the sample code
.h file

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

    @interface PGSecondViewController : UIViewController <CLLocationManagerDelegate>

    {
    NSTimer *Timer;
    IBOutlet UILabel *displayTimer;
    int ticks;

    }

    @property (nonatomic, retain) IBOutlet UILabel *displayTimer;

    -(IBAction)timerStart:(id)sender;
    -(IBAction)timerStop:(id)sender;
    -(void)updateTimer:(NSTimer *)theTimer;

    @end

and heres the .m file

#import "PGSecondViewController.h"

@interface PGSecondViewController ()

@end

@implementation PGSecondViewController

@synthesize displayTimer;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    UITabBarItem *tbi = [self tabBarItem];
    [tbi setTitle:@"Start"];

    // Custom initialization
}
return self;
}


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[displayTimer setText:@"00:00:00"];

}

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}  

-(IBAction)timerStart:(id)sender;
{
Timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self     selector:@selector(updateTimer:) userInfo:nil repeats:YES];


}
-(IBAction)timerStop:(id)sender;
{
[Timer invalidate];
Timer = nil;
}

-(void)updateTimer:(NSTimer *)theTimer
{
if (ticks == nil)
{
    ticks = 0;
}
else
{
    ticks = ticks + 1;
}
[displayTimer setText:[NSString stringWithFormat:@" %d ", ticks]];

}


@end

Basically what I get is the timer starting once and NSLabel displays 0 but after that nothing happens. I have tried it with [[NSRunLoop currentRunLoop] addTimer:Timer forMode:NSDefaultRunLoopMode] and [[NSRunLoop currentRunLoop] run] after scheduling the timer but it doesn’t help.

Any ideas??

  • 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-17T08:34:35+00:00Added an answer on June 17, 2026 at 8:34 am

    Your if(ticks == nil) is broken; ticks is an int, not an object. Its initial value will be 0.

    I just fixed by changing that method a bit, and everything works fine.

    -(void)updateTimer:(NSTimer *)theTimer
    {
        ticks++;
        [displayTimer setText:[NSString stringWithFormat:@" %d ", ticks]];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into this problem a few times and I'm not happy with
Basically i have a problem with this timer program I am trying to put
I have the following java.util.Timer but it does not seem to execute and I
Was using Java Timer, Then switched to ScheduledExecutorService, but my problem is not fixed.
I have faced the same problem many times. The Same Problem was With This
UPDATE ViewController is not destroyed, new ViewController is not created. This: TimerViewController * timerViewController
We have an SP timer job that was running fine for quite a while.
Problem: find ids that are in one file but not in another. Each file
Here's some background of my problem(its not homework). I have a 4 items to
I have some code that will be accessed from two threads: class Timer{ public:

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.