我正在创建一个简单的IOS应用程序将倒数计时器。当我在模拟器中运行应用程序时,倒数计时器不会倒计时。它只停留在当前时间。
关于使数字排成一行的任何建议都会很好。
我的.h文件
// ViewController.h
// ******
//
// Created by ***** on 7/29/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
NSDate *destinationDate;
IBOutlet UILabel *datelabel;
NSTimer *timer;
}
@end
我的.m文件
//
// ViewController.m
// ******
//
// Created by ******* on 7/29/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "ViewController.h"
@implementation ViewController
-(void) updateLabel {
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calender components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[datelabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c", [components day], ' ', [components hour], ' ', [components minute], ' ', [components second], ' ']];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
destinationDate = [[NSDate dateWithTimeIntervalSince1970:1343609103] retain];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:NO];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
答案 0 :(得分:0)
将重复YES
设置为您的计时器。
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
编辑:还可以考虑使用NSDateFormatter来显示字符串。
如果您想改进布局,我建议将NSDateFormatter与多个UILabel结合使用。日期格式化程序可用于创建所需格式的字符串,每个标签可以保存日期/时间字符串的特定部分。看一下Apple's Data Formatting Guide最低调的部分是Unicode Technical Standard #35的链接,它定义了您可以使用的所有格式。
分割完日期后,您只需按照自己的意愿布置视图即可。