Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count down timer - iPhone [closed]

Tags:

ios

iphone

I want to display count down timer. I have the start date and end date. I need to display the remaining time like

days : hours : minutes : seconds

How can I do this?

like image 406
Dev Avatar asked Nov 22 '12 04:11

Dev


2 Answers

you can set coundown like my below code :-

 -(void) viewWillAppear:(BOOL)animated
 {
 timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
 }

AND

 -(void) updateCountdown 
 {

NSString *dateString = @"14-12-2012";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];


NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];



NSDateComponents *componentsHours = [calendar components:NSHourCalendarUnit fromDate:now];
NSDateComponents *componentMint = [calendar components:NSMinuteCalendarUnit fromDate:now];    
NSDateComponents *componentSec = [calendar components:NSSecondCalendarUnit fromDate:now];        


NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsDaysDiff = [gregorianCalendar components:NSDayCalendarUnit
                                                            fromDate:now
                                                              toDate:dateFromString
                                                             options:0];



lblDaysSetting.text=[NSString stringWithFormat:@"%02d",componentsDaysDiff.day];
lblHouresSetting.text=[NSString stringWithFormat:@"%02d",(24-componentsHours.hour)];    
lblMinitSetting.text=[NSString stringWithFormat:@"%02d",(60-componentMint.minute)];  
lblSecSetting.text=[NSString stringWithFormat:@"%02d",(60-componentSec.second)];  



 }

now just set your logic

its code output as my project as bellow::-

enter image description here

like image 87
Nitin Gohel Avatar answered Oct 22 '22 23:10

Nitin Gohel


this is the code for .h file:

@interface UIMyContoller : UIViewController {

NSTimer *timer;
IBOutlet UILabel *myCounterLabel;
}

@property (nonatomic, retain) UILabel *myCounterLabel;
-(void)updateCounter:(NSTimer *)theTimer;
-(void)countdownTimer;

@end

and here is the code for .m file:

@implementation UIMyController
@synthesize myCounterLabel;

int hours, minutes, seconds;
int secondsLeft;

- (void)viewDidLoad {
[super viewDidLoad];

secondsLeft = 16925;
[self countdownTimer];
}

- (void)updateCounter:(NSTimer *)theTimer {
if(secondsLeft > 0 ){
    secondsLeft -- ;
    hours = secondsLeft / 3600;
    minutes = (secondsLeft % 3600) / 60;
    seconds = (secondsLeft %3600) % 60;
    myCounterLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}
else{
    secondsLeft = 16925;
}
}

-(void)countdownTimer{

secondsLeft = hours = minutes = seconds = 0;
if([timer isValid])
{
    [timer release];
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
[pool release];
}

hope this helps. happy coding

adrian

like image 28
Adrian P Avatar answered Oct 22 '22 23:10

Adrian P