NSDatePicker

Discussion in 'Mac Programming' started by ulbador, Jan 6, 2011.

  1. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #1
    So I'm having a problem with a NSDatePicker.

    Basically, when using this code from the protocol, it takes "two clicks" for the date to actually change in the control.


    Code:
    
    - (void)loadSchedule {
            NSLog(@"Date is %@",[datePicker dateValue]);
    }
    - (void)datePickerCell:(NSDatePickerCell *)aDatePickerCell validateProposedDateValue:(NSDate **)proposedDateValue timeInterval:(NSTimeInterval *)proposedTimeInterval {
            NSLog(@"PDate: %@",*proposedDateValue);
    	[self loadSchedule];
    }
    
    Basically, on the first "click" on a new date, it prints the new date and the old date (once from the datePickerCell override and once from the loadSchedule method). On the second click, it prints the new date twice.

    About the only other solution I could think of would be to assign the proposedDate to a separate date field, and reading from that one instead of straight from the control itself. That honestly seems a little nutty and unnecessary though.
     
  2. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #2
    This method
    datePickerCell:validateProposedDateValue:timeInterval:​
    gives you the chance to validate the date before it gets changed. The NSLog in that method should show the new value. Then you call your loadSchedule method before this method has returned, so the NSLog in loadSchedule should show the old value. The next time you click, assuming you don't change the date, the new value will already be set, so you get the new value printed twice.

    I think this looks ok so far, so is your program not doing what you wanted, or is it just the output of the NSLog statements that looks wrong to you?
     
  3. ulbador thread starter macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #3
    Actually, given that behaviour, my code is working exactly as it should. For my purpose though, I just want to be able to detect the initial change.

    Looking over the delegate, it doesn't look like Apple provides a method to do this. On to plan C! (storing the proposedDateValue in a separate variable and reading that instead of [myDatePicker dateValue]).

    Plan B involved setting the datePicker dateValue to the date from this delegate method, which caused a nasty little bit of recursion that actually crashed XCode.
    Thanks!
     
  4. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #4
    I don't think you've shared enough of your program to see the big picture of what you're trying to do here.

    If your plan B was to set the NSDatePicker's dateValue from the proposed date value in the delegate method, then you just don't need to do that. The mouse click sets its value to whatever the user chooses.

    Have a look at this sample code of the use of NSDatePicker. It uses that same delegate method that can override the changing of the value of the NSDatePicker and it looks like this:

    Code:
    // -------------------------------------------------------------------------------
    //	datePickerCell:aDatePickerCell:proposedDateValue:proposedTimeInterval
    //
    //	Delegate to NSDatePickerCell
    // -------------------------------------------------------------------------------
    - (void)datePickerCell:(NSDatePickerCell *)aDatePickerCell validateProposedDateValue:(NSDate **)proposedDateValue timeInterval:(NSTimeInterval*)proposedTimeInterval
    {	
    	MyWindowController *controller = (MyWindowController *)[aDatePickerCell delegate];
    	
    	if ((controller == self) && (aDatePickerCell == [datePickerControl cell]))
    	{
    		// override the date and time?
    		if ([[overrideDateCheck cell] state])
    		{
    			// override the date using the user specified date
    			*proposedDateValue = [overrideDate dateValue];
    		}
    	}
    }
    
    
    There's a checkbox to set whether to override the date that's set or not.
     
  5. ulbador thread starter macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #5
    I had actually found that example.

    What I was trying to accomplish was simple. Date clicked equals action done. For my purposes, I was using the GUI Calendar type layout, so there wasn't really any validation that it needed to be doing. Basically, when the user clicked the date, it needed to do something with the date the user selected from a GUI calendar.

    My problem was that I was trying to read the new date value out of the control before it was actually committed, so silly me, I tried to read it straight out of the control. Given that delegate is pretty thin, I thought that was the only option left to me.

    Pretty easily fixed after I rescrewed my head on straight:

    Code:
    - (void)loadSchedule:(NSDate *) datePicked {
            NSLog(@"Date is %@",datePicked);
    }
    - (void)datePickerCell:(NSDatePickerCell *)aDatePickerCell validateProposedDateValue:(NSDate **)proposedDateValue timeInterval:(NSTimeInterval *)proposedTimeInterval {
            NSLog(@"PDate: %@",*proposedDateValue);
    	[self loadSchedule:*proposedDate];
    }
    
    Thanks!
     
  6. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #6
    Ok, that will work.

    Its not the only way to do it though. You could bind the NSDatePicker to an NSDate instance variable of one of your own custom classes. Then write your own setProperty method. This will of course be called every time the date changes and you can call whatever you want done from there.

    I'm sure there's other ways of doing it too, if anyone else wants to join in here?
     

Share This Page