Search code examples
iosobjective-cnstimer

On DetailViewController,countdown


I have a countdown timer it working in cell. I’m pushing cell and shown DetailView Controller. On DetailViewController, countdown not working. For example, in cell my countdown shown look like | 23 days 14 hours 45 min 33 sec | and working. I’m pushing cell and on DetailViewController shown label like | 23 days 14 hours 45 min 32 sec | but not working.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.label.text = self.tweetMessage;
    self.username.text = self.userId;
    self.count.text = self.countdownStartTime;

    NSTimer *timer;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(updateTime)
                                           userInfo:nil
                                            repeats:YES];
}

-(void)updateTime
{
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSUInteger preservedComponents = (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay);
    self.datePicker.date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:self.datePicker.date]];

    //Get the time left until the specified date
    NSInteger ti = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
    NSInteger seconds = ti % 60;
    NSInteger minutes = (ti / 60) % 60;
    NSInteger hours = (ti / 3600) % 24;
    NSInteger days = (ti / 86400);

    //Update the lable with the remaining time
    self.count.text = [NSString stringWithFormat:@"%02li days %02li hours %02li min %02li sec", (long)days, (long)hours, (long)minutes, (long)seconds];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"Continue"]){
        DetailViewController *controller = (DetailViewController *)segue.destinationViewController;
        controller.tweetMessage = ((TWTTweetTableViewCell *)sender).tweetMessage.text;
        controller.userId = ((TWTTweetTableViewCell *)sender).user.text;
        controller.countdownStartTime = ((TWTTweetTableViewCell *)sender).countdownTime.text;   
    }
}

Solution

  • import "DetailViewController.h"

    @interface DetailViewController ()

    @end

    @implementation DetailViewController

    • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view.

      self.label.text = self.tweetMessage; self.username.text = self.userId; self.count.text = self.countdownStartTime;

      NSTimer *timer; timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; }

    -(void)updateTime {

    //Get the time left until the specified date
    NSInteger ti = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
    NSInteger seconds = ti % 60;
    NSInteger minutes = (ti / 60) % 60;
    NSInteger hours = (ti / 3600) % 24;
    NSInteger days = (ti / 86400);
    
    //Update the lable with the remaining time
    self.countdownTime.text = [NSString stringWithFormat:@"%02li days %02li hours %02li min %02li sec", (long)days, (long)hours, (long)minutes, (long)seconds];
    

    }

    enter code here
    
    Not working my Friend..