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

elorc

macrumors newbie
Original poster
Apr 1, 2009
12
0
I've noticed this with a few projects actually. The first was a small project I was writing from scratch that incorporated a datepicker. I noticed that no matter what I tried to do, it would always show as 2/12/1982. I'd try to override it by using something like this:

Code:
NSDate *currentDate = [NSDate date];
[myDatePickerObj setValue:currentDate];

In place of setValue: I've also tried setDateValue: with the same results. No luck.

Also, in the Hillegass book's chapter 11 exercise, I saw the same behavior. In this particular example, he has the reader create a new class to override NSArrayController's newObject method.

CarArrayController.h
Code:
#import <Cocoa/Cocoa.h>

@interface CarArrayController : NSArrayController {
}

@end

CarArrayController.m
Code:
#import "CarArrayController.h"

@implementation CarArrayController

- (id)newObject
{
	id newObj = [super newObject];
	NSDate *now = [NSDate date];
	[newObj setValue:now forKey:@"datePurchased"];
	
	return newObj;
}

@end

When I run the application and create a new record, the datepicker still shows as 2/12/1982. Any ideas?
 

elorc

macrumors newbie
Original poster
Apr 1, 2009
12
0
Ok I fixed the problem in the Hillegass example. I screwed up and didn't have the CanArrayController class set to the array controller I was using. I still can't figure out why the other code doesn't work. I'm putting it in my init override:

Code:
- (id)init
{
   self = [super init];
   userlist = [[NSMutableArray alloc] init];
	
   // Set the date to today
   NSDate *newDateVal = [NSDate date];
   [myDatePicker setDateValue:newDateVal];
   NSLog(@"The current date is %@", newDateVal);

   return self;
}
 

elorc

macrumors newbie
Original poster
Apr 1, 2009
12
0
What happens if you comment out the message to myDatePicker?

If I comment it out, I get a warning that says newDateVal is an unused variable. The debug console shows my NSLog line: "The current date is 2009-04-19 12:50:54 -0400"
 

lucasgladding

macrumors 6502
Feb 16, 2007
319
1
Waterloo, Ontario
Try obtaining the dateValue from the NSDatePicker instance. I don't have Cocoa Programming in front of me, but I would guess that your outlet isn't set. When I have problems with setter methods, I usually try logging the result of the getter method first. That will tell you if the NIB has been loaded at that time and whether the connection has been made.
 

JoshDC

macrumors regular
Apr 8, 2009
115
0
I'm fairly sure your problem is you're doing these initializations in the wrong place. If you're doing it how I think you are, when the init method is called datePicker isn't yet initialized itself, so the setValue: method is send to a null object. You can set a breakpoint and hover over datePicker to confirm this (it should be 0x0). Put interface initializations in the -(void)awakeFromNib method instead.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.