0

我正在创建一个简单的 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
4

1 回答 1

0

将重复设置YES为您的计时器。

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];

编辑:还考虑使用NSDateFormatter来显示您的字符串。

如果你想改进你的布局,我建议将 NSDateFormatter 与多个 UILabel 结合使用。日期格式化程序可用于创建您需要的格式的字符串,每个标签可以包含日期/时间字符串的特定部分。查看Apple 的数据格式指南,其中最低调的部分是指向Unicode 技术标准 #35的链接,它定义了您可以使用的所有格式。

拆分日期后,您只需根据需要布置视图。

于 2012-07-30T01:40:01.447 回答