PDA

View Full Version : Clock/Time App - Objective-C




haiggy
Apr 1, 2009, 05:53 PM
Hey,
I'm trying to make an app that displays the time at the top of the screen (for the iPhone --> Objective-C).

I've successfully been able to do this using NSDate and output it to a UILabel however when I do it this way the year, milliseconds and timezone correction also shows up. Is there a way to just get the hours and minutes? Also I assume I need an NSTimer to refresh the view every minute so that the clock updates every minute instead of staying at the time it opens at, correct? How would I go about implementing this?

Finally, I was thinking of maybe using my own graphics for each number... how much does that complicate things?

Any help in any regard would be appreciated,
I know you guys are very helpful :)

Thanks!



eddietr
Apr 1, 2009, 06:14 PM
Those are kind of broad questions, but here are some thoughts at least:


There is an iPhone Programming forum, this probably belongs there
You might want to look at NSDateFormatter. It gives you a lot of flexibility in formatting dates and times the way you want. (Or the way the user wants depending on his/her locale)
NSTimer is a way to keep the clock updated. I would look at the docs for that, and try it. And if you run into problems with that, post your code here and somebody will help.


Hope that helps.

haiggy
Apr 1, 2009, 08:07 PM
Hey,
thanks for the reply. I'll look into the NSDateFormatter.


Mods could you maybe move this thread? :p

Anybody else? :)

haiggy
Apr 2, 2009, 03:33 PM
#import "TimeViewController.h"

@implementation TimeViewController
@synthesize time, dateFormat;


-(void)viewDidLoad;
{
NSDate *dateTime = [NSDate date];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateStyle:NSDateFormatterShortStyle]; // This needs fixing up
NSString *clock = [[NSString alloc] initWithFormat:@"%@", dateTime];
time.text = clock;
[clock release];

[NSTimer scheduledTimerWithTimeInterval:100.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

-(void) onTimer;
{
//What goes here?
}

Any help guys? :o

eddietr
Apr 2, 2009, 04:44 PM
Any help guys? :o

So it seems like you create a dateFormatter, but then you don't actually use it. So that's one thing you'll want to fix. (plus, you probably want to hold onto that DateFormatter since you'll be using it so many times. No sense in recreating it every time. It seems you already have a property to do just that.)

And then in your call back at the end of the timer (onTimer), you just need to set your label again to the current time.

haiggy
Apr 2, 2009, 05:03 PM
So it seems like you create a dateFormatter, but then you don't actually use it. So that's one thing you'll want to fix. (plus, you probably want to hold onto that DateFormatter since you'll be using it so many times. No sense in recreating it every time. It seems you already have a property to do just that.)

And then in your call back at the end of the timer (onTimer), you just need to set your label again to the current time.

Haha I realize I don't use it... but the issue would be... how exactly DO I use it? :confused: I'm a noob.

But ok I will update the NSTimer, thanks.

eddietr
Apr 2, 2009, 05:33 PM
Haha I realize I don't use it... but the issue would be... how exactly DO I use it? :confused: I'm a noob.


Take a look at the Data Formatting Programming Guide for Cocoa. There are specific examples there of using a DateFormatter.

haiggy
Apr 3, 2009, 04:31 PM
Take a look at the Data Formatting Programming Guide for Cocoa. There are specific examples there of using a DateFormatter.

Thanks a lot for your help. That document was exactly what I needed but somehow I found something different on Date Formatting from the Apple Documentation. Just one final question -- I promise! :)

#import "TimeViewController.h"

@implementation TimeViewController
@synthesize time, date;


-(void)viewDidLoad;
{
// Time
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setDateStyle:NSDateFormatterNoStyle];
[timeFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSDate *stringTime = [NSDate date];
NSString *formattedDateStringTime = [timeFormatter stringFromDate:stringTime];
time.text = formattedDateStringTime;

// Date
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *stringDate = [NSDate date];
NSString *formattedDateStringDate = [dateFormatter stringFromDate:stringDate];
date.text = formattedDateStringDate;

// Timer
[NSTimer scheduledTimerWithTimeInterval:100.00 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];


}

-(void) onTimer;
{
time.text = formattedDateStringTime; // Does not work
[time setText:(NSString *)formattedDateStringTime]; // Also does not work
// What exactly should go here.... will this also update the date when it changes?
}


Here's my code... it works, as in the date and time display exactly how I want them to, they just don't update. I'm lost as to the format/style whatever I have to use to put in the -(void) onTimer in able for it to actually listen to me and update....

Thanks! :D

haiggy
Apr 4, 2009, 01:54 PM
Hey,
I just pasted all my code from retrieving the date and time into the timer...... and it worked! I didn't think it was that simple....... did i go about this the wrong way? atleast it works....

thanks for all the help

edit: Ohh.. I see I can remove all the code from viewDidLoad and just put in the OnTimer.... just keep the timer in the viewDidLoad... makes a lot more sense now...

lee1210
Apr 4, 2009, 02:19 PM
Did you get the value to persist between runs with NSUserDefaults? If so, you might add a button to "clear" and start again.

Now might be a good time to paste all of your code here for the benefit of others, also.

-Lee

haiggy
Apr 4, 2009, 03:19 PM
Did you get the value to persist between runs with NSUserDefaults? If so, you might add a button to "clear" and start again.

Now might be a good time to paste all of your code here for the benefit of others, also.

-Lee

Not sure what you mean, I just wanted to make the date and time show up in a label and keep increasing by seconds instead of just being static.

@implementation TimeViewController
@synthesize time, date;

-(void) onTimer:(NSTimer *)timer;
{
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setDateStyle:NSDateFormatterNoStyle];
[timeFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSDate *stringTime = [NSDate date];
NSString *formattedDateStringTime = [timeFormatter stringFromDate:stringTime];
time.text = formattedDateStringTime;

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *stringDate = [NSDate date];
NSString *formattedDateStringDate = [dateFormatter stringFromDate:stringDate];
date.text = formattedDateStringDate;

}


-(void)viewDidLoad;
{
// Timer
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
}