我正在制作一款与考试有关的应用程序,因此当考试开始时,时间应该随之开始。例如30分钟,并且它应该以29:59的方式递减。
我该如何实现这个功能?
请问有人可以给我一个示例或易于跟随的逐步教程吗?
我正在制作一款与考试有关的应用程序,因此当考试开始时,时间应该随之开始。例如30分钟,并且它应该以29:59的方式递减。
我该如何实现这个功能?
请问有人可以给我一个示例或易于跟随的逐步教程吗?
这段代码用于创建一个倒计时计时器。
.h文件的代码。
@interface UIMyContoller : UIViewController {
NSTimer *timer;
IBOutlet UILabel *myCounterLabel;
}
@property (nonatomic, retain) UILabel *myCounterLabel;
-(void)updateCounter:(NSTimer *)theTimer;
-(void)countdownTimer;
@end
.m文件的代码。
@implementation UIMyController
@synthesize myCounterLabel;
int hours, minutes, seconds;
int secondsLeft;
- (void)viewDidLoad {
[super viewDidLoad];
secondsLeft = 16925;
[self countdownTimer];
}
- (void)updateCounter:(NSTimer *)theTimer {
if(secondsLeft > 0 ) {
secondsLeft -- ;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
myCounterLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
} else {
secondsLeft = 16925;
}
}
-(void)countdownTimer {
secondsLeft = hours = minutes = seconds = 0;
if([timer isValid]) {
[timer release];
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
[pool release];
}
希望这能对你有所帮助。
timer = ...;
这一行 - 在-(void)countdownTimer
中的其他所有内容都可以安全省略。 - temporary_user_name