Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

haiggy

macrumors 65816
Original poster
Aug 20, 2003
1,328
76
Ontario, Canada
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

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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

macrumors 65816
Original poster
Aug 20, 2003
1,328
76
Ontario, Canada
Hey,
thanks for the reply. I'll look into the NSDateFormatter.


Mods could you maybe move this thread? :p

Anybody else? :)
 

haiggy

macrumors 65816
Original poster
Aug 20, 2003
1,328
76
Ontario, Canada
Code:
#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? :eek:
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
Any help guys? :eek:

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

macrumors 65816
Original poster
Aug 20, 2003
1,328
76
Ontario, Canada
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.
 

haiggy

macrumors 65816
Original poster
Aug 20, 2003
1,328
76
Ontario, Canada
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! :)

Code:
#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

macrumors 65816
Original poster
Aug 20, 2003
1,328
76
Ontario, Canada
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors 65816
Original poster
Aug 20, 2003
1,328
76
Ontario, Canada
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.

Code:
@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];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.