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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:27:34+00:00 2026-05-23T20:27:34+00:00

I have the following code for a stopwatch function. I have 2 UIButtons. One

  • 0

I have the following code for a stopwatch function.

I have 2 UIButtons. One is for start/stop, the other is for reset.

I want to combine these 2 into just 1 button, that functions as a start button, then while the timer is running, it will be a stop button, that when pushed, resets the timer.

How can I modify this code to reflect this?

- (IBAction)startStop:(UIButton *)sender 
{
    if ( self.myTimer ) 
    {
        [self.myTimer invalidate];
        self.myTimer = nil;

        [sender setTitle:@"Start" forState:UIControlStateNormal];
    } 
    else 
    {
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
        [sender setTitle:@"Stop" forState:UIControlStateNormal];
    }
}

- (void)handleTimer:(NSTimer *)timer 
{
    self.counter--;
    self.timerLabel.text = [NSString stringWithFormat:@"%ld", self.counter];

    if ( self.counter <= 0 )
    {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        [self.myTimer invalidate];
        self.myTimer = nil;
        self.timerButton.enabled = NO;
    }
}

- (IBAction)reset:(id)sender 
{
    self.timerButton.enabled = YES;
    self.counter = self.counterSegment;
    self.timerLabel.text = timerCount;
    self.timerButton.titleLabel.text = @"Start";
    self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
}
- (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index
{
    if ( self.myTimer ) 
    {
        [self.myTimer invalidate];
        self.myTimer = nil;
        self.timerButton.titleLabel.text = @"Start";
        self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    }   
    else
    {
        self.timerButton.enabled = YES;
        self.counter = self.counterSegment;
        self.timerLabel.text = timerCount;
        self.timerButton.titleLabel.text = @"Start";
        self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    }
    if (index == 0)
    {
        NSLog(@"15 sec");
        self.timerCount = @"15";
        self.counterSegment = 15;
    }
    else if (index == 1)
    {
        NSLog(@"30 sec");
        self.timerCount = @"30";
        self.counterSegment = 30;
    }
}
  • 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-23T20:27:35+00:00Added an answer on May 23, 2026 at 8:27 pm

    Steps you could do:

    1. remove the reset button from you UI;

    2. modify the startStop method so that when pressed while the timer is already running, it will send a reset message to itself:

    I think it should be enough. Have a look at this code:

    - (IBAction)startStop:(UIButton *)sender 
    {
    if ( self.myTimer ) 
    {
        [self.myTimer invalidate];
        self.myTimer = nil;
    
        [self reset:sender];   //-- LINE ADDED
    
        [sender setTitle:@"Start" forState:UIControlStateNormal];
    } 
    else 
    {
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
        [sender setTitle:@"Stop" forState:UIControlStateNormal];
    }
    }
    

    Since in reset you are not using the sender argument, you can remove it and have:

    - (void)reset
    {
    self.timerButton.enabled = YES;
    self.counter = self.counterSegment;
    self.timerLabel.text = timerCount;
    self.timerButton.titleLabel.text = @"Start";
    self.timerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    }
    

    In this way you will be able to use this method in a more generic way.

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

Sidebar

Related Questions

So I have the following code block: var sw = new Stopwatch(); sw.Start(); while
I have following code to serialize my data into a file: out = new
I have following code in WPF XAML and want it to be converted to
I have following code I want to test: public class MessageService { private MessageDAO
I have following code snippet: self.xmlHttpReq = new XMLHttpRequest(); self.xmlHttpReq.onreadystatechange = function() { if(self.xmlHttpReq.readyState
I have following code to send files to a FTP server. function FtpUploader( [string]$uri,
I have following code: <div class='parent'> <div class='left-child'></div> <div class=right-child></div> </div> What I want
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have following code in my application: // to set tip - photo in

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.