PDA

View Full Version : Quick iPhone SDK noob Questions.




jmac1074
Apr 24, 2008, 05:10 PM
I have a few questions:

1) How can I use the UILabel class to display an Integer?

2) How can I get the current time for use in my app?

Thanks.

Jmac



MDMstudios
Apr 24, 2008, 05:42 PM
Well, I just uploaded a tutorial onto http://iphonedevcentral.org/ on how to show a int with a UILabel, but it will take a few hours for it to get processed.

As for telling the time, I believe there is a type of NS object you can use to get the time.

mysticwhiskey
Apr 24, 2008, 06:24 PM
I have a few questions:

1) How can I use the UILabel class to display an Integer?

2) How can I get the current time for use in my app?

Thanks.

Jmac

To display an int in a UILabel, you convert it to an NSString and set the value of the UILabel's 'text' property:

UILabel *myLabel = [[UILabel alloc] init];
int myInt = 1234;
myLabel.text = [NSString stringWithFormat:@"%d", myInt];


(Ref: https://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UILabel_Class/Reference/UILabel.html)



To get the current date/time, use the 'date' class method of NSDate:

NSDate *now = [NSDate date];


Edit: I just re-read your post, and it seems you're asking for the current time (not including the day/month/year?). If so, you can use an NSDateFormatter to do the job:


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString* currentTime = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];


Note: There's a few different style constants you can use (instead of NSDateFormatterShortStyle) to get different time formats, but if you want exact control over the time format, you can specify your own format strings.

(Ref: http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html)

jmac1074
Apr 24, 2008, 06:55 PM
thanks. this really helped a lot.

jmac1074
Apr 24, 2008, 07:08 PM
Also, do you know if there is any way to parse the date into integers? like one int for hours, one for minutes, and one for seconds?

mysticwhiskey
Apr 24, 2008, 08:09 PM
Also, do you know if there is any way to parse the date into integers? like one int for hours, one for minutes, and one for seconds?

Yes, here's one way (there might be a better way, but this works):


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy"];
int year = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter setDateFormat:@"MM"];
int month = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter setDateFormat:@"dd"];
int day = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter setDateFormat:@"HH"];
int hour = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter setDateFormat:@"mm"];
int minute = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter setDateFormat:@"ss"];
int second = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter release];


Note: when getting the hour, I'm using 'HH' to specify 24-hour time; 7pm would become an int of value 19

mysticwhiskey
Apr 25, 2008, 02:32 AM
I found another way to do this (possibly more efficient, but I'm not sure) using the NSDateComponents class:


NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];

NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];

[calendar release];

stadidas
Apr 25, 2008, 05:00 AM
You can use the dayOfWeek:, dayOfMonth etc methods of NSCalendarDate (a subclass of NSDate) to easily get the values you are after.

soch
Aug 4, 2010, 08:32 PM
You can use the dayOfWeek:, dayOfMonth etc methods of NSCalendarDate (a subclass of NSDate) to easily get the values you are after.

Legacy API: NSCalendarDate
This class does not exist in iPhone OS and is deprecated in Mac OS X.