2012-07-30 163 views
0

我正在創建一個簡單的IOS應用程序倒數計時器。當我在模擬器中運行應用程序時,倒數計時器不倒計時。它只是停留在當前時間。Xcode倒數計時器不倒計時(NSCalender)

enter image description here

而且對製作數字排隊任何建議將是很好。

我.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

組重複YES你的計時器。

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

編輯:也考慮使用NSDateFormatter顯示您的字符串。

如果你想改善你的佈局,我會建議使用NSDateFormatter與多個UILabels結合使用。日期格式化程序可用於創建所需格式的字符串,並且每個標籤可以包含日期/時間字符串的特定部分。看看Apple's Data Formatting Guide最低調的部分是指向Unicode Technical Standard #35的鏈接,它定義了您可以使用的所有格式。

一旦你分割你的日期,你只需要佈置你的意見,只要你喜歡。

+0

好吧,我把它設置爲是,它的工作原理.....任何建議排列它? – Techboy3005 2012-07-30 14:50:49

+0

我已經更新了我的答案。 – Jessedc 2012-07-30 22:16:25