Search code examples
iosnsdatenstimernstimeinterval

How to create NSDate timer with selector and time interval


Hi I want to create an NSDate object with the following functionality as the NSTimer line below.

[NSTimer scheduledTimerWithTimeInterval:(slider.value * 60) target:self selector:@selector(nextFunction) userInfo:nil repeats:NO];

So what is it that appeals to me about the above like is that I can put in the time I want for it to countdown. Also once the timer has reached 0 the nextFunction method is called. But I need to use NSDate for a future operation that cannot be completed with NSTimer. In summary, I want to create an NSDate that will countdown from whatever current value my slider holds then upon completion of countdown to call the nextFunction method. Thanks


Solution

  • You could create the NSDate from the sliders value:

    NSDate *date = [[NSDate alloc] initWithTimeintervalSinceNow:(slider.value * 60)];
    

    Then use its timeInterval to create the timer:

    [NSTimer scheduledTimerWithTimeInterval:[date timeIntervalSinceNow] target:self selector:@selector(nextFunction) userInfo:nil repeats:NO];
    

    I guess that should do it.