-1

Hello fellow programmers. I am currently trying to develop a workout timer. Before the user presses the "Start" button, he/she sets the number of seconds and minutes with a stepper.

Let's imagine that I set the workout's total length to 5 minutes and 30 seconds. Unfortunately, the timer starts at 30 seconds. In actuality, I am trying to make it start 5:30 minutes. I'm trying to fix something in the method. Here's the code for the header file:

#import <UIKit/UIKit.h>

@interface AutoLayoutViewController : UIViewController
// 1. The three green labels
// Workout's timer
{
    IBOutlet UILabel *workoutTimer;
    NSTimer *workoutCountdown;
    int remainingTime;
}


// 2. The stepper

// Stepper for Workout's total length
@property (strong, nonatomic) IBOutlet UIStepper *secondsWorkoutChanged;


// 4. The button
@property (strong, nonatomic) IBOutlet UIButton *resetButton;

Secondly, here's the code for the implementation file:

#import "AutoLayoutViewController.h"

@interface AutoLayoutViewController ()

@end

// Variables associated with label called Workout's total length
int seconds;
int minutes;

@implementation AutoLayoutViewController

// 1. Steppers

// Stepper for Workout's length
- (IBAction)secondsWorkoutChanged:(UIStepper *)sender {

    /* User increases value of seconds with stepper. Whenever variable for seconds is equal or greater than 60, the program sets the value of minutes through this division: seconds / 60. */
    seconds = [sender value];
    int minutes = seconds / 60;


    /* "If" statement for resetting seconds to 0 in order for the label to look like a watch. REAL number of seconds stored by stepper modulus operated by 60.
     */
    if (seconds > 59) {
        seconds = seconds % 60;
    }

    [workoutTimer setText: [NSString stringWithFormat:@"%2i : %2i", (int) minutes, (int) seconds]];

}




// 2. The button

// Method for countdown

- (void)chrono:(NSTimer *)timer
{

    seconds = seconds -= 1;

    workoutTimer.text = [NSString stringWithFormat: @"%2i : %2i", minutes, seconds];

   if (seconds <= 0) {
        [workoutCountdown invalidate];
    }
}

 //Start button
-(IBAction) startPauseButton:(UIButton *)sender {

    workoutCountdown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(chrono:) userInfo:nil repeats:YES];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

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

@end

All in all, I've been trying to code the "Start" button and the chrono method so that the countdown takes heed of the real number of minutes and seconds set by the user. However, it's been to no avail so far. I would like to express my gratitude to whomever helps me.

  • Please be more specific about the problem, and narrow down the code necessary to reproduce it. – jscs Jan 13 '14 at 04:08
  • the problem is little confusion , do you mean if you set 5:30 mins timer then you dont want that 5 mins and timer should be of only 30 seconds – V-Xtreme Jan 13 '14 at 04:16
  • What I meant is that when I set the timer at 5:30 mins, it starts only at 30 seconds. I've been trying to make the timer start at 5:30 mins, for instance. – Anh Khoi Do Jan 13 '14 at 11:52
  • @Josh Caswell: Following your request, I edited my message in order to narrow my code's sample to the part I've been trying to fix. From what I found, the problem is in the implementation file and it is in the part called "Method called for countdown" (see comments in the code's sample). Thanks for your time! – Anh Khoi Do Jan 13 '14 at 11:59
  • I can see that u have not added the logic to implement the 'minutes' in the countdown. Are you trying to say that the value of 'minutes' is '0' ? – GoGreen Jan 13 '14 at 12:02
  • @GoGreen: Indeed, I'm trying to figure out a way to implement the logic for the "minutes" in the countdown method. I confirm that if I set the timer, for instance, to 5:30 minutes, the value of the variable "minutes" unfortunately becomes '0' after I've pressed "Start". – Anh Khoi Do Jan 13 '14 at 12:06

1 Answers1

0

The reason why the minutes variable is reset to 0 is that, it has been re - initialized in the method:

- (IBAction)secondsWorkoutChanged:(UIStepper *)sender {

      seconds = [sender value];
      int minutes = seconds / 60;

Replace the line :

int minutes = seconds / 60;

with the line:

minutes = seconds / 60;

Also, here is a logic that I suggest to implement the minutes countdown:

- (void)chrono:(NSTimer *)timer
{
     seconds = seconds -= 1;
     workoutTimer.text = [NSString stringWithFormat: @"%2i : %2i", minutes, seconds];

     if (seconds <= 0) {
         if (minutes <= 0) {
             [workoutCountdown invalidate];
         }
         else {
             seconds = 60;
             minutes -= 1;
         }
     }
}

Hope this helps!

GoGreen
  • 2,251
  • 1
  • 17
  • 29
  • I would like to express my gratitude for your help! When I set the timer at 5:30 minutes, the `Start` button makes the timer be initially set at 5:30 minutes. Speaking about the stepper, I was wondering what is the advantage of erasing the term `int`, which was placed next to the variables `minutes`? Once again, thanks a lot! – Anh Khoi Do Jan 13 '14 at 22:39
  • The variable `minutes` was declared initially as `int minutes;`. Such variables that are available privately throughout the class are called `instance` variables. Re-declaring `minutes` in `int minutes = seconds/60` allocates a fresh memory location for the variable and its previous value is lost. This was the reason why it always had a value of `0`. – GoGreen Jan 14 '14 at 04:58
  • You may check out this [**stackoverflow answer**](http://stackoverflow.com/questions/12632285/declaration-definition-of-variables-locations-in-objectivec) for further clarifications. – GoGreen Jan 14 '14 at 05:01