2016-03-09 69 views
1

當前時間格式應該和hh:mm:ss一樣,秒數應該運行,秒數不應該停止計數,它應該保持運行我向你展示我寫的代碼。我想在時間格式爲hh:mm的標籤上顯示當前時間:ss:

-(void)timeNotify 
{ 
NSDate *currDate = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setDateFormat:@"hh:mm:ss a"]; 
NSString *dateString = [dateFormatter stringFromDate:currDate]; 

_timeLbl.text=dateString; 

} 

,並鑑於沒有負載我打電話這

-(void)viewDidLoad 
{ 
    [self timeNotify]; 
} 
+0

到底是什麼問題? – Avi

+0

當我運行應用程序時,標籤顯示格式爲hh:mm:ss的當前日期,但問題是時間不變。它只會在您刷新應用程序時發生變化。我希望一旦應用程序啓動,時間應該會不斷變化。意思秒數應該繼續改變00,01,02,03等等。我希望你得到它..? – techno

+0

可能的重複這個http://stackoverflow.com/questions/15615637/how-to-get-local-time-on-ios –

回答

4

你應該使用NSTimer

- (void)viewDidLoad 
{ 
    [NSTimer scheduledTimerWithTimeInterval:1.0 
            target:self 
            selector:@selector(timeNotify) 
            userInfo:nil 
            repeats:YES]; 
} 

- (void) timeNotify 
{ 
    NSDate *currentTime = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"hh:mm:ss"]; 
    NSString *resultString = [dateFormatter stringFromDate: currentTime]; 
    _timeLbl.text = resultString; 
} 
+0

感謝戴安娜Prodan,爲解決我的問題。 ..它真的有效.. – techno

+0

請接受我的答案,如果它解決了您的問題 –

+0

我已經upvoted你... – techno

-2
NSDate *today = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings 
[dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
NSString *currentTime = [dateFormatter stringFromDate:today]; 
NSLog(@"User's current time in their preference format:%@",currentTime); 
+0

它不會工作兄弟。 – techno

+0

我沒有給予反對票,一旦確切地檢查了問題,提問者需要計時器 –

+0

@ Anbu.Karthik我還沒有投票給你,親愛的。 – techno

-1

,如果你想你的時間定期更新

NSTimer* timer =  [NSTimer scheduledTimerWithTimeInterval:1 
            target:self 
           selector:@selector(timeNotify) 
           userInfo:nil 
            repeats:YES]; 

記得設置timer到您應該使用NSTimernil當視圖消失,因爲定時器保持對你的引用視圖控制器,所以它會導致內存泄漏,如果你不破壞它

+0

感謝您的回答.... – techno

+0

如果它解決了您的問題,不要忘記回答並將其標記爲您的決議;)thx – CZ54

+0

我已upvoted你.. – techno

相關問題