Search code examples
objective-cnstimercountdown

NSTimer days countdown and sending to label


I have a button to trigger NSTimer to countdown 3 days. How do I get a label showing 2 days left or 0 days left in the tableviewCell. and also I want to color the cell with red color when 0 days left. And in the cell there is a button to reset the timer for another 3 days. Objective-c. Thanks in advance!


Solution

  • Store the timer expired Day in the User Defaults. This you may do this in an IBAction method to set the new Expiring Date. Running this action again resets the value.

       NSInteger expiredInDays = 3;
    
    // Calculate the Date.
    // Get the Date
    NSDate * now = [NSDate date];
    NSTimeInterval  tiNow = [now timeIntervalSinceReferenceDate];
    tiNow = tiNow + 60*60*24*expiredInDays;
    NSDate * expires = [NSDate dateWithTimeIntervalSinceReferenceDate:tiNow];
    [[NSUserDefaults standardUserDefaults] setObject:expires forKey:@"Expired Date"];
    

    Ask for the expiring Day where you need it in your code.

    NSDate *expiredDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"Expired Date"];
    NSLog(@"Expires in: %@",expiredDate);