开发者

NSTimer countdown label

It will keep counting down and updating the current time. Anyone has any 开发者_运维知识库idea how to approach this problem? I don't see how the NStimer will recognize the date format like this: 20110803 23:59:59


Look up NSDateFormatter. You can specify a format string (-[setDateFormat:])that lets you convert to AND from NSDate and NSString.

When converting back to your countdown view, you may want to use NSDateComponents and NSCalendar to get the pieces you need for your countdown label (instead of NSDateFormatter). You could do something like:

NSDateComponents *countdown = [[NSCalendar currentCalendar] 
    components:(NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) 
      fromDate:[NSDate date] 
        toDate:expiration 
       options:0];

This will calculate all the unit differences for you (with respect to the device's currently configured calendar settings).

You can get the components back with a call like [countdown day] or [countdown hour].


You can get the date easily by using NSDateFormatter like this to get a NSDate object from your string.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd' 'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [dateFormatter dateFromString:your_date_string];


You could convert the date from NSString to NSDate by using the NSDateformatter class.

NSString *dateString = @"20110803 23:59:59";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];  

Now, in order to count down, you use the NSTimer class to fire every one second, then you can get the difference in seconds between the current date and your datevariable by simply use timeIntervalSinceDate.

NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:yourDate];

Now that you have the difference in seconds, you convert it to whatever format you wish to display.


I didn't drill down into this problem deeply, therefore sorry, if my answer is a bit unappropriate.

In this case I would use NSDate. Using NSDateFormatter I would convert text data of xml to NSDate. Changing the value of NSDate, when every tick of NSTimer, properly. And a bit of code, 'cause the symbols of NSDateFormatter sometimes create mess:

    NSString *xmlDateFormat = @"yyyyMMdd HH:mm:ss";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:xmlDateFormat];
    NSDate *yourDate = [dateFormatter dateFromString:timeStringFromXml];

This is for reading from xml. For changing value of NSDate when tick you can see the doc of NSDate. And for display the value in label I would arithmetically calculate every value and show it with the help of casual NSString:

   [NSString stringWithFormat:@"%dday %dh %dm %ds", days, hours, minutes, seconds];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜