iphone - 倒数计时器 - iPhone

标签 iphone ios

<分区>

我想显示倒数计时器。我有开始日期和结束日期。我需要像这样显示剩余时间

天:小时:分钟:秒

我该怎么做?

最佳答案

你可以像我下面的代码一样设置倒计时:-

 -(void) viewWillAppear:(BOOL)animated
 {
 timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
 }

 -(void) updateCountdown 
 {

NSString *dateString = @"14-12-2012";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];


NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];



NSDateComponents *componentsHours = [calendar components:NSHourCalendarUnit fromDate:now];
NSDateComponents *componentMint = [calendar components:NSMinuteCalendarUnit fromDate:now];    
NSDateComponents *componentSec = [calendar components:NSSecondCalendarUnit fromDate:now];        


NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsDaysDiff = [gregorianCalendar components:NSDayCalendarUnit
                                                            fromDate:now
                                                              toDate:dateFromString
                                                             options:0];



lblDaysSetting.text=[NSString stringWithFormat:@"%02d",componentsDaysDiff.day];
lblHouresSetting.text=[NSString stringWithFormat:@"%02d",(24-componentsHours.hour)];    
lblMinitSetting.text=[NSString stringWithFormat:@"%02d",(60-componentMint.minute)];  
lblSecSetting.text=[NSString stringWithFormat:@"%02d",(60-componentSec.second)];  



 }

现在只需设置你的逻辑

它的代码输出为我的项目如下::-

enter image description here

关于iphone - 倒数计时器 - iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13505837/

相关文章:

iphone - 使用 CoreData 创建静态库时出现问题 - 无法使用 nil 模型创建 NSPersistentStoreCoordinator

iphone - opengl 1.1 在启动时搞砸了(随机)

iphone - 限制 iPhone 状态栏中的通知数量

iphone - Objective-c 类初始值设定项执行多次

ios - 使用 ejabberd(版本 2.1.6)在 iPhone 中推送通知延迟

ios - 从另一个类调用的类函数

ios - 打开和关闭特定 viewController 的声音

iphone - 在 Xcode 4 中添加 socket

ios - removeFromSuperview() 不起作用

objective-c - 我可以在 Objective C 中使用枚举作为属性吗