Hello其他程序员。我目前正在尝试开发锻炼计时器。在用户按下“开始”按钮之前,他/她使用步进器设置秒数和分钟数。
让我们想象一下,我将锻炼的总长度设定为5分30秒。不幸的是,计时器从30秒开始。实际上,我试图让它从5:30开始。我正在尝试修复方法中的某些内容。这是头文件的代码:
#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;
其次,这是实现文件的代码:
#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
总而言之,我一直在尝试编写“开始”按钮和计时方法,以便倒计时注意用户设置的实际分钟数和秒数。然而,到目前为止一直无济于事。我想对任何帮助我的人表示感谢。
答案 0 :(得分:0)
minutes
变量重置为0
的原因是,它已在方法中重新初始化:
- (IBAction)secondsWorkoutChanged:(UIStepper *)sender {
seconds = [sender value];
int minutes = seconds / 60;
替换行:
int minutes = seconds / 60;
行:
minutes = seconds / 60;
此外,这是一个我建议实施minutes
倒计时的逻辑:
- (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;
}
}
}
希望这有帮助!