2016-02-23 29 views
0
viewdidload 

{ 
    NSDateFormatter *df = [[[NSDateFormatter alloc] init] init]; 
    [df setDateFormat:@"HH:mm:ss"]; 

    ctime=([df stringFromDate:[NSDate date]]); 
    NSDate *date1 = [df dateFromString:@"12:44"];` 
    NSDate *date2 = [df dateFromString:@"20:50"]; 
    NSTimeInterval interval = [date2 timeIntervalSinceDate:date1]; 
    int hours = (int)interval/3600;    // integer division to get the hours part 
    int minutes = (interval - (hours*3600))/60; // interval minus hours part (in seconds) divided by 60 yields minutes 
    NSString *timeDiff = [NSString stringWithFormat:@"%d:%02d", hours, minutes]; 
    currHour=hours; 
    currMinute=minutes; 
    currSeconds=00; 
    [self start]; 
} 

-(void)start 
{ 
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES]; 

} 
-(void)timerFired 
{ 
    if((currHour>0 || currMinute>0 || currSeconds>=0) && currMinute>=0) 
    { 
     if(currSeconds==0) 
     { 

      currMinute-=1; 
      currSeconds=59; 
     } 
     else if(currSeconds>0) 
     { 

      currSeconds-=1; 
     } 
     if(currMinute>-1) 


enter code here 
      [self.timeonelabel setText:[NSString stringWithFormat:@"%d%@%d%@",currHour,@":",currMinute,@":"]]; 
     //[self.hourlabel setText:[NSString stringWithFormat:@"%d%@",currHour,@":"]]; 
      // [self.timeonelabel setText:[NSString stringWithFormat:@"%d%@%d%@",00,@":",currMinute,@":"]]; 
     [self.timetwolabel setText:[NSString stringWithFormat:@"%02d",currSeconds]]; 
    } 
    else 
    { 
     [self.timer invalidate]; 
    } 
} 

在傳遞當前時間和結束時間的計時器應該運行倒計時爲0 .Problem的定時器沒有運行倒計時,請幫幫我。我甚至停留在顯示計時器倒計時tableview ..請幫助如何倒數計時器上的時間和分鐘和停止時,它的0,目標C

+0

IF((currHour> 0 || currMinute> 0 || currSeconds> = 0)&& currMinute> = 0)執行? – Signare

+0

是執行,currHour不需要編輯代碼。 –

+0

你在哪裏縮短了時間?一個週期完成後,什麼都不會發生? @Shilpa M – Signare

回答

1

你有一些錯誤的事情在你的代碼中從字符串創建日期。首先,您在NSDateFormatter中指定的日期格式和您使用的實際格式不匹配 - 一個有秒,另一個沒有。這意味着dateFromString將返回零。此外,你也應該指定一個日期而不是唯一的時間,否則你不能確定你會得到今天的日期。

最後,通過提取並跟蹤原始變量中的所有NSDate組件,您都過於複雜。我寧願做這樣的事情:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
    [df setDateFormat:@"YYYY/MM/dd HH:mm"]; 
    self.endTime = [df dateFromString:@"2016/02/23 11:00"]; 

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES]; 
} 

-(void)timerFired { 
    if ([[NSDate date] compare:self.endTime] == NSOrderedAscending) { 
     NSLog(@"still going"); 
     // enter code here 
     // Use NSDateComponentsFormatter to show what you need to show 
    } 
    else { 
     NSLog(@"done"); 
     [self.timer invalidate]; 
    } 
} 
+0

好吧,這看起來不錯。讓我檢查一下。 如何讓定時器在每個單元的結束時間不同的表視圖中運行? –

+0

我認爲做這件事最簡單的方法是將UITableViewCell繼承並移動NSTimer和它的功能,以便每個單元都有它自己的功能。當然,如果你打算做更多的事情,那麼簡單地顯示一些值,這可能違反了MVC模式,以便在單元格中有這麼多的邏輯,這是一個視圖。 – pajevic

+0

第二個想法是,在UITableViewCell中執行它可能不是一個好主意,因爲單元格被重用並且維護定時器會變得太複雜。 「UITableViewController」中有一組定時器(每個單元一個)可能更安全。 – pajevic

1
try this code maybe this is your requirement 


- (void)viewDidLoad 
{ 
    NSCalendar *g = [[ NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 


    NSDateComponents *d_comp1 = [[NSDateComponents alloc]init]; 
    // lower value for time compare to d_comp time 
    [d_comp1 setHour: 1]; 
    [d_comp1 setMinute: 1]; 
    [d_comp1 setSecond: 00]; 


    NSDateComponents *d_comp = [[NSDateComponents alloc]init]; 
    enter code here 
    [d_comp setHour: 10]; 
    [d_comp setMinute: 1]; 
    [d_comp setSecond: 10]; 


    NSDate *date1 = [g dateFromComponents: d_comp1]; 

    NSDate *date2 = [g dateFromComponents: d_comp]; 


      NSTimeInterval interval = [date2 timeIntervalSinceDate:date1]; 
     int hours = (int)interval/3600;    // integer division to get the hours part 
     int minutes = (interval - (hours*3600))/60; // interval minus hours part (in seconds) divided by 60 yields minutes 
     int sec = (int)d_comp1.second - (int)d_comp.second; 
    if (sec<0) { 
     sec+=60; 

    } 
     currHour=hours; 
     currMinute=minutes; 
     currSeconds= sec; 
     [self start]; 
} 

-(void)start 
{ 
    timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES]; 

} 
-(void)timerFired 
{ 
     if(currHour>0 || currMinute>0 || currSeconds >0) 
     { 

      if (currSeconds < 0) { 
       currSeconds = 60; 
       currMinute -= 1; 
      } 
      if (currMinute < 0) { 
       currHour -= 1; 
       currMinute = 59; 
       currSeconds =60; 
      } 
      [self.timeonelabel setText:[NSString stringWithFormat:@"%d:%d:%d",currHour,currMinute,currSeconds]]; 
      currSeconds--; 
     } 
     else 
     { 
      [self.timeonelabel setText:[NSString stringWithFormat:@"%d:%d:%d",currHour,currMinute,currSeconds]]; 

      [timer invalidate]; 
     } 
}