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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:27:44+00:00 2026-05-24T06:27:44+00:00

I am writing a iPhone app that times how long the user is holding

  • 0

I am writing a iPhone app that times how long the user is holding onto the button. When the user ‘Touches Down’ the buttonPressed method is fired and when the button is realeased aka no longer being held the NSTimer is supposed to invalidate. However, it is not and I am having a extremely difficult time finding a solution. Hope you guys can help! Here’s the code:

-(IBAction)buttonReleased:(id)sender {


[stopWatchTimer invalidate];

NSString *label0 = @"0";
[labelText setText:label0];
score--;

NSString *scoreMessage =[[NSString alloc] initWithFormat:@"YOU LOSE! Your score was: %i",score];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"GAME OVER!" message:(NSString *)scoreMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

[alert show];
[alert release];

score = 0;
tap = 0;

}

- (void)updateTimer
{
    static NSInteger counter = 0;
    labelText.text = [NSString stringWithFormat:@"%d", counter++];
    score++;

}

-(IBAction)buttonPressed:(id)sender {


        stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/1.0 target:self selector:@selector(updateTimer)userInfo:nil repeats:YES];
        tap++;

}
  • 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-24T06:27:45+00:00Added an answer on May 24, 2026 at 6:27 am

    What actually calls buttonReleased:? There are several ways that a button can stop being held down. The touch up might be inside or outside of the button for instance.

    My strong suspicion is that buttonPressed: is called more often than buttonReleased:. Because you’re directly accessing your ivars rather than using accessors, you’re not handling this correctly, and you’re probably leaving around old timers when you assign a new timer.

    First, always use accessors. Don’t mess with ivars directly (the exceptions are in init, dealloc and in accessors themselves). Many problems like this one come from messing with ivars directly. (Plus, it makes the code confusing. Is score a local, global or ivar?)

    Second, an NSTimer setter should look like this:

    - (void)setTimer:(NSTimer *)aTimer {
      if (aTimer != timer_) {
        [timer_ invalidate];
        [timer_ release];
        timer_ = [aTimer retain];
      }
    }
    

    This ensures that when you replace a timer, you invalidate the old one. The if() check is important on the off-chance that you call the equivalent of self.timer = self.timer. If you did that without an if() check you would invalidate the timer when you didn’t mean to.

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

Sidebar

Related Questions

I am writing an iPhone app that allows you to browse images in one
I'm writing an iPhone App that relies on getting the device location. Management have
I have an iPhone app that I am writing and it has 2 targets
I have a 32bit iMac that I am writing an iPhone app in Xcode
I am writing an app that interfaces with the iPhone address book. Here is
I am writing an app that requires the user's current location (lastknownlocation won't be
I'm writing an iPhone app that is mainly centered around grid patterns, so I
I am writing an iPhone app that needs to be able to upload photos
Writing an iPhone app, and I'm getting my data from a REST API that
I'm writing an iPhone app that can be started via a custom URL. So,

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.