PDA

View Full Version : Set NSDatePicker to System Time




stadidas
Jul 28, 2006, 10:22 AM
At Uni last year I wrote a program in pure Java to help manage my money. Recently I've decided to re-write this program using Cocoa and Objective-C.
I've worked out most of the back-end of it, but there's one thing I need to sort out first.
Here is the prototype interface I've been working on:
http://www.matefeedkillrepeat.com/assetmonitor.png

I would like to be able to set the top NSDatePicker to the current system date. I would then use the value from the user-definable NSDatePicker at the bottom to calculate how long is left in the current term.
If anyone knows how to set an NSDatePicker to the current system time I would be grateful if you let me know!



caveman_uk
Jul 28, 2006, 10:35 AM
Just looking at the docs have you tried

[datePickerObject setDateValue:[NSDate date]];

stadidas
Jul 28, 2006, 10:43 AM
I shall give that a try when I get down to coding it.
By the way, Beer Alchemy looks awesome! One of my mates at Uni is buying a MacBook Pro soon, and he brews beer, so he'll be buying a license, he looked impressed by your app.

stadidas
Jul 28, 2006, 02:10 PM
I've made a test project to try and get this NSDatePicker setting to work.
I have an NSDatePicker called currentDate defined in my header file. I have written a method called setDate which I call from the + (void)Initialize function. The setDate function looks like this:

- (void)setDate
{
[currentDate setDateValue:[NSDate date]];
}


I call it from the Initialize function by doing [self setDate]; but I get a warning and it doesn't seem to work. I'm new to this Objective C lark and am probably doing something very stupid. Anyone know what I'm doing wrong?

kpua
Jul 28, 2006, 03:41 PM
Perhaps if you told us what the warning you're getting is, we could help you easier...

Also, it looks like you have your class's +initialize method called +Initialize, which isn't the same thing, and it's probably never getting called since you're not overriding the correct method.

caveman_uk
Jul 28, 2006, 03:43 PM
By the way, Beer Alchemy looks awesome!

Ta very much!:D

I call from the + (void)Initialize function

Is there any reason why you're using that? I'd do it in -init. Do you know the difference between the - and + methods. (Just asking)?

caveman_uk
Jul 28, 2006, 03:44 PM
Also, it looks like you have your class's +initialize method called +Initialize, which isn't the same thing, and it's probably never getting called since you're not overriding the correct method.
Good point...I didn't spot that.

stadidas
Jul 28, 2006, 04:55 PM
Ta very much!:D

Is there any reason why you're using that? I'd do it in -init. Do you know the difference between the - and + methods. (Just asking)?
I am using the initialize function, shouldn't have capitalised it.
The - methods are instance methods, and + are class methods I think. Still not sure whether they're supposed to be called functions or methods (I tend to think of them as methods as I come from a Java background).

I've now put this code in:

- (void) init
{
[self setDate];
}


I get an warning saying: 'Converter' may not respond to '-setDate'. When I try and run the app the XCode run log says:
"[Session started at 2006-07-28 22:53:29 +0100.]
2006-07-28 22:53:30.330 NSDatePickerTest[1309] *** -[NSCFArray addObject:]: attempt to insert nil"

kpua
Jul 28, 2006, 06:19 PM
Looks like you've got two problems that may or may not be connected.

First, the compiler can't find a -setDate method for your Converter class. Did you declare one in a .h file?

Second, you think you're adding something to an array, but you're not (you're trying to add nil instead of a valid pointer). I would break on -[NSException raise] in the debugger to see why that pointer is nil.

Catfish_Man
Jul 28, 2006, 08:05 PM
+initialize is almost certainly a bad time to be doing stuff like that. Try registering as NSApp's delegate and doing it in -applicationDidFinishLaunching.

alexhct
Nov 7, 2008, 01:41 AM
I am using the initialize function, shouldn't have capitalised it.
The - methods are instance methods, and + are class methods I think. Still not sure whether they're supposed to be called functions or methods (I tend to think of them as methods as I come from a Java background).

I've now put this code in:

- (void) init
{
[self setDate];
}


I get an warning saying: 'Converter' may not respond to '-setDate'. When I try and run the app the XCode run log says:
"[Session started at 2006-07-28 22:53:29 +0100.]
2006-07-28 22:53:30.330 NSDatePickerTest[1309] *** -[NSCFArray addObject:]: attempt to insert nil"

I think, you have following 3 issues: I had those previousely
1. in your header file, you need to declare the setDate method like
-(void) setDate;
this will help for the warmming at compile time

2. in the init function, you need to return the object
please note, you don't set time here, the datepicker obj is not ready yet
-(id)init
{
return self;
}

3. add another method in your .m file and run your setDate here
-awakeFromNib
{
[self setDate];
}

try above three steps. :)