About Me

My photo
Dhaka, Bangladesh
I am B.S.C Engineer,CSE,SUST and Ex-Cadet of Mirzapur Cadet College.

Wednesday, May 2, 2012

Dealing with date: A Countdown Clock

My intention is to build small code segments that I may need to us when I need to build bigger projects.

I am creating an application that shows a countdown a 24 hour from present time (I am following http://appsamuck.com/).

Step1:  I created a simple View Based Application "MinutesToMidnight" on Xcode

Step2: I double clicked on the Resource-> MinutesToMidnightViewController.xib

Step3: Added a UILabel where the countdown shall be displayed and also selected the view and changed its color property. Also, changed the font of the label.

Step4: Declared IBOutlet for the label in the MinutesToMidnightViewController and drag and made it recognized to the file's owner. Actually, as I want to control the label to change its value programmatically, I need to tell the Interface builder that it is the label I will be referring about.



@interface MinutesToMidnightViewController : UIViewController 
IBOutlet UILabel *lblClock;

@property (nonatomic,retain) UILabel *lblClock;

-(void) updateLabel; 


@synthesize lblClock;

Step5: I need a timer. Well, I will declare a NSTimer on AppDelegate(??). But Why in app delegate and why NOT in the view controller?? I am not much sure. But, I assume that putting the timer available in app delegate will help the other view controllers(If we had more) reuse it as timer task is an expensive operation.

So, in app delegate.h I declared an NSTimer variable and in .m file I initialized it in application didFinishLaunching method.

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

Also, I declared a method (onTimer)in .h file and implemented it in .m file of the app delegate.

What I wrote in method named onTimer?

[viewController updateLabel];

What does it mean?? It means send a message to viewContoller(Look in .h file of app delegate and we shall find that it is a pointer reference to MinutesToMidnightViewController) to call a method named updateLabel. So, we in future shall write a method in view controller in that name. 

What is a selector?? Let me google it first.

In Objective-C, selector has two meanings. It can be used to refer simply to the name of a method when it’s used in a source-code message to an object. It also, though, refers to the unique identifier that replaces the name when the source code is compiled. Compiled selectors are of type SEL. All methods with the same name have the same selector. You can use a selector to invoke a method on an object—this provides the basis for the implementation of the target-action design pattern in Cocoa.

Step6: Now we need to declare updateLabel the method on .h file of viewcontroller implement the method on .m file

NSDate* now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];
NSInteger hour = 23 - [dateComponents hour];
NSInteger minute = 59 - [dateComponents minute];
NSInteger second = 59 - [dateComponents second];
[gregorian release];

lblClock.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute,second];

Lets explain the code. We have an NSDate and NSCalendar variable. NSCalendar instance is initialized as a gregorian calendar. 

NSDateComponent variable took the format(Hour, minute and second) from that NSCalendar instance and specific date is derived from NSDate instance. As we are calculating hour, minute and second from a fixed countdown point, 24 hours to present time, we deducted 23 from hour from date Component hour and 59 minute and 59 seconds from minute and date respectively.

Afterwards we associated it with the text of the label in which we are showing the time. 

Step7: Now Lets Build and run and it should look like this

N.B. We need to invalidate and release the timer in applicationWillTerminate and dealloc
respectively in the .m file of app delegate

No comments: