gpt4 book ai didi

iPhone SDK : Countdown timer

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:45 25 4
gpt4 key购买 nike

请帮忙。我想使用 iphone sdk 创建倒数计时器。我有当前时间:

NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
NSString *currentTime = [dateFormatter stringFromDate:today];
[dateFormatter release];

来自数据库的时间。例如:当前时间是 19:18:30,数据库时间是 19:20:01。所以计时器应该设置为 0:01:31 并开始倒计时。当到达 0:00:00 时,从数据库中获取另一个时间(例如:19:21:00),设置计时器(0:00:59)并再次开始倒计时。当到达 0:00:00 时,取... 计时器应由 UILabel 显示。

请帮忙。它开始让我发疯。 :)

最佳答案

试试这个。我刚刚只编写了解决方案所需的方法。不要忘记实现 dealloc 等。

倒计时 View Controller .h

  @interface CountdownViewController : UIViewController {     
IBOutlet UILabel *clockLabel;
NSDate *databaseDate;
NSTimer *timer;
}

@property(nonatomic, retain) IBOutlet UILabel *clockLabel;
@property(nonatomic, retain) NSDate *databaseDate;

@end

CountdownViewController.m

@implementation CountdownViewController

@synthesize clockLabel;
@synthesize databaseDate;

- (void) showClock {
NSTimeInterval remainingSec = [databaseDate timeIntervalSinceNow];
if (!timer || remainingSec <= 0) {
[timer invalidate];
timer = nil;
// getting time from database
self.databaseDate = [NSDate dateWithTimeIntervalSinceNow:20.0];
remainingSec = [databaseDate timeIntervalSinceNow];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(showClock)
userInfo:nil
repeats:YES];
}

NSInteger hours = remainingSec / 3600;
NSInteger remainder = ((NSInteger)remainingSec)% 3600;
NSInteger minutes = remainder / 60;
NSInteger seconds = remainder % 60;

clockLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}

@end

关于iPhone SDK : Countdown timer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7463163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com