tags:

views:

523

answers:

3

I have a nice chunk of reuable code I'm trying to edit and keep clean of the details. I'd like to be able to edit the NSDate some NSTextField is bound to. The only thing I have is the IBOutlet to the text field. Is this possible?

A: 

I assume you tried directly setting the object value of the text field.

If that didn't work, use infoForBinding: to find out what property of what object it's bound to, then use setValue:forKeyPath: to set the value at that property of that object.

Peter Hosey
A: 

I suppose if you knew the date format that the textField is using you could:

NSDateFormatter *myformatter = [[NSDateFormatter alloc] init];
[myformatter setDateFormat:textFieldDateFormat];
textField.text = [myformatter stringFromDate:yourdate];

If you don't know the format then you could try parsing the testField.text to figure it out, but I that's way past my pay grade.

pat
A: 

Setting the objectValue for the NSTextField by itself does not get down to the model.

I'll try to explain what I'm doing. In my app I started with a simple textfield that has an NSDateFormatter attached to it. It was bound to an NSDate and all was well. Later I wanted to add a popup calendar next to the text field so a user could click their date.

http://media.clickablebliss.com/billable/interface_experiments/new_date_picker.png

Now rather than hard coding this behavior I tried to be generic with the solution. I ended up writing a custom helper object. I would connect the helper to the textfield and the button on screen and then set the action of the button to openPopup: in the helper. When the user popped up a calendar view my code would bind the calendar view to the same object that the textfield was bound to with the following code:

// Set the binding of the date picker in the panel to the same binding as the controlToBeHelped
NSDictionary *controlToBeHelpedBindings = [controlToBeHelped infoForBinding:@"value"];
[popupPanelDatePicker bind:@"value" 
                  toObject:[controlToBeHelpedBindings valueForKey:NSObservedObjectKey]
               withKeyPath:[controlToBeHelpedBindings valueForKey:NSObservedKeyPathKey] 
                   options:nil];

This works great in my one project. In my new project I'd like to reuse this code with a twist. This time the NSDate can be nil. I'd like to agument my code so that if the user clicks on the button to pick a date when the date is currently nil I first set it to now and then show the popup.

Thus, I need to edit the NSDate this control is bound to via an IBOutlet. Thanks.

EDIT: Thanks to Peter I think I got this working:

if ([[controlToBeHelpedBindings valueForKey:NSObservedObjectKey] valueForKeyPath:[controlToBeHelpedBindings valueForKey:NSObservedKeyPathKey]] == nil) {
 [[controlToBeHelpedBindings valueForKey:NSObservedObjectKey] setValue:[NSDate date] forKeyPath:[controlToBeHelpedBindings valueForKey:NSObservedKeyPathKey]];
}
zorn
Then you'll have to use infoForBinding: to discover the model, then use KVC to store the new date at that property. (Also, why'd you use a different account?)
Peter Hosey
(Post-edit) Yeah, my advice hasn't changed. Get the observed object and key path, use [observedObject valueForKeyPath:observedKeyPath] to check for nil, then use [observedObject setValue:[NSDate date] forKeyPath:observedKeyPath] if necessary.
Peter Hosey
Thanks this works great.
zorn