PDA

View Full Version : NSdatepicker




MrFusion
Aug 2, 2006, 10:36 AM
I am trying to understand nsdatepicker, but the object is quite strange. Strange as in lack of methods. I have set the thing to graphical mode and I can set the date (it gets a blue color) and I can select another date (grey color). But what I can't do, is get the newly selected date (when I have set a date) for use somewhere else or select e.g. an entire week, let alone get all the dates back from the nsdatepicker object.

I am looking for similar behaviour as in iCal.

Thanks



robbieduncan
Aug 2, 2006, 10:49 AM
It's very likely that iCal does not use this control. Almost the entire iCal app UI uses custom controls that are specific to iCal (use FScript Anywhere to check).

You currently cannot use this control to select ranges of dates. From the documentation (http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSDatePickerCell_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40001575-CJBIAHGF), which I'm sure you read posting right:

NSRangeDateMode

Allow selection of a range of dates. (Not currently implemented.)

Available on Mac OS X v10.4 and later.

kpua
Aug 2, 2006, 11:24 AM
What Robbie says about the date range mode is correct. It's not currently implemented, but Apple doesn't usually leave documented API in a non-working state for very long, so I would bet we see that feature in Leopard.

As for your statement:
get the newly selected date (when I have set a date) for use somewhere else
I think you're just not looking hard enough. NSDatePicker is a subclass of NSControl, which means you can set an action on it, which is fired when the date is changed. You can access the date by sending the -dateValue method to it. So, the following method could be hooked up to the date picker's action to print the date to the console:


- (IBAction)printDate:(id)sender {
NSLog(@"The selected date is: %@", [sender dateValue]);
}

MrFusion
Aug 3, 2006, 02:28 AM
You currently cannot use this control to select ranges of dates
Yes, I saw that, but since iCal has something like that I thought it would be possible in some (other) way.



I think you're just not looking hard enough. NSDatePicker is a subclass of NSControl, which means you can set an action on it, which is fired when the date is changed. You can access the date by sending the -dateValue method to it.

I am working on a second program now, so still relatively new to cocoa. I didn't expect that calling dateValue would have a different outcome if it's used with an action, as in your example.

Thanks

kpua
Aug 3, 2006, 10:12 AM
I didn't expect that calling dateValue would have a different outcome if it's used with an action, as in your example.

I'm not sure what you mean by this. Perhaps I wasn't very clear. -dateValue doesn't have a different outcome in the context of an action. Whether or not you call it in the action, it always returns the current date displayed by the date picker. I was just pointing out that because the date picker is a control, you can hook up an action method to it so you can perform whatever actions you want whenever the date is changed.

MrFusion
Aug 4, 2006, 03:00 AM
I'm not sure what you mean by this. Perhaps I wasn't very clear. -dateValue doesn't have a different outcome in the context of an action. Whether or not you call it in the action, it always returns the current date displayed by the date picker. I was just pointing out that because the date picker is a control, you can hook up an action method to it so you can perform whatever actions you want whenever the date is changed.

I did the following (which will show you I am new to cocoa). I set the date programmatically to the current date. I have a button that will read the date when pushed. I push the button and get the current date back. I change the date and push again. The current date is still returned.

With your code it returns the selected date.

So I didn't expect the same function to have a different outcome when used in a different way, or else I must have done something else wrong. Anyway, it doesn't matter anymore, since it works now.