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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:24:39+00:00 2026-05-31T07:24:39+00:00

I have created a NSTimer In Xcode 4.2 and it works but i get

  • 0

I have created a NSTimer In Xcode 4.2 and it works but i get this one problem.

here is my project in the simulator

simulator

when i press start it starts and when i press stop it stops and when its stopped it will reset
but when it starts and i press reset when it is going nothing happens it don’t reset when started basically you have to stop then reset is the ways and this or do i need to add code any where heres a copy of my code.

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController {

    IBOutlet UILabel *time; 

    NSTimer *myticker;

    //declare baseDate
    NSDate* baseDate; 

}

-(IBAction)stop;
-(IBAction)reset;

@end

heres my implementation

 #import "FirstViewController.h"

@implementation FirstViewController

@synthesize baseDate;


-(IBAction)start {
    [myticker invalidate];
    self.baseDate = [NSDate date];
    myticker = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
}

-(IBAction)stop;{ 

    [myticker invalidate];
    myticker = nil;

}



-(IBAction)reset {
    self.baseDate = [NSDate date];
     time.text = @"00:00:0";  
}


-(void)showActivity {
    NSTimeInterval interval = [baseDate timeIntervalSinceNow];
    double intpart;
    double fractional = modf(interval, &intpart);
    NSUInteger hundredth = ABS((int)(fractional*10));
    NSUInteger seconds = ABS((int)interval);
    NSUInteger minutes = seconds/60;

    time.text = [NSString stringWithFormat:@"%02d:%02d:%01d", minutes%60, seconds%60, hundredth];
}

I Really Appreciate It. Thanks.

  • 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-31T07:24:40+00:00Added an answer on May 31, 2026 at 7:24 am

    First, the above should crash with EXC_BAD_ACCESS when it reaches showActivity since baseDate is not being retained in the start method. [NSDate date] returns an autoreleased object so baseDate will have an invalid reference after the start method.

    I suggest changing baseDate to a retain property and then setting it in start using self.:

    //.h
    @property (nonatomic, retain) NSDate *baseDate;
    
    //.m
    @synthesize baseDate;
    
    -(IBAction)start {
        [myticker invalidate];
        self.baseDate = [NSDate date];
        myticker = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
    }
    

    To fix the reset problem, note that the showActivity method takes the current value of baseDate to calculate the elapsed time and then sets the time label to display it formatted.

    In the start method, you set the baseDate to the current time (you don’t set time.text) and then start the timer. The showActivity method will then keep firing and set time.text.

    In the reset method, you want the timer to start showing the elapsed time since the moment reset is pressed. The timer is already running so you don’t need to re-start it. Setting the time label text doesn’t work because when the already-running timer fires again, it will calculate the elapsed time from baseDate which is still the original start time and then set time.text based on that. So instead of setting time.text, set the baseDate:

    -(IBAction)reset {
        self.baseDate = [NSDate date];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so here is the problem i have, i created two NSTimer objects and they
I have created sample circular UIScrollview.it works fine.but i want to scroll through NSTimer
I have created an app that uses NSTimer, which gets triggered each second. My
I have created a custom dialog for Visual Studio Setup Project using the steps
I have created a voice recording app. It works fine on an iPhone 3G
I have created a simple command line tool that outputs hello world. This is
I have created a NSTimer and call it every 15 seconds. Method that I
I have a problem when creating an NSTimer without repeat. Imagine I create a
I have an NSTimer that I init with this code: testTimer = [[NSTimer alloc]
I have created a music app that plays one of 9 sounds when an

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.