试图创建一个倒数计时器到某个日期,但我希望它以天而不是月、天显示事物(比如,2 个月 2 天)我宁愿让它显示实际天数,比如“62 天”我应该怎么做更改代码?谢谢。
这是我用于倒计时的代码。
#import "CountdownViewController.h"
@interface CountdownViewController ()
@end
@implementation CountdownViewController;
-(void)updateLabel;
{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
char units = NSDayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[dateLabel setText:[NSString stringWithFormat:@"%dDays", [components day]]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
destinationDate = [NSDate dateWithTimeIntervalSince1970:1356393600];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
// Do any additional setup after loading the view, typically from a nib.
}
问题是它只是向我展示了日子部分。我玩了代码以到达上面的代码,但它曾经是这样的
-(void)updateLabel;
{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
char units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[dateLabel setText:[NSString stringWithFormat:@"%d %d %d %d %d", [components month], [components day], [components hour], [components minute], [components second]]];
}