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.