Question

Why property's value bound to NSDatePicker does not always reflect what control shows? There are at least 2 cases when my bound property gets different value than date picker shows:

  1. Initial value. Date picker shows it's initial date (set in IB) but bound property returns nil (if user does not interact with picker).
  2. Min value changes date in picker, but bound property still returns old value (the value user had set before min value was set).

What is the reason to use bindings over target/action if it does not support such fundamental behaviour? I'm new to Cocoa binding so maybe I'm missing something.


Update:

Attached sample project to see the problem.

Was it helpful?

Solution

Why property's value bound to NSDatePicker does not always reflect what control shows?

In Model-View-Controller pattern, your NSDatePicker is the view, your ViewController is the controller, and the NSDate property is the model.

You bind a view to a model via a controller. Not the other way round.

When you bind a view to a model via a controller, the view will start reflecting the model. If you interact with the view (e.g., change the date), the change will be applied to the model.

Initial value. Date picker shows it's initial date (set in IB) but bound property returns nil (if user does not interact with picker).

NSDatePicker shows whatever default value, because the model is not providing an NSDate object. NSDatePicker should NOT change the model without user interaction.

Min value changes date in picker, but bound property still returns old value (the value user had set before min value was set).

NSDatePicker just shows a default value within the range of acceptable values, because the model is not providing an NSDate object.

What is the reason to use bindings over target/action if it does not support such fundamental behaviour?

Target/action is just part of what bindings does automatically. Without bindings, you would create an action method in your controller (the target) to handle user interaction (e.g., user changing the date). In the action method, you would then change the model object. Also, on loading the view, you would sync the view to what you have in your model. Bindings eliminates a lot of this kind of code.

OTHER TIPS

NSDatePicker cannot display an empty date. If you do not set the dateValue or bind the value to nil the control still displays a date value; it likes 12/02/1982.

Not being able to display an empty date and thus indicate a nil binding is irritating.

The following NSDatePicker subclass can show an empty date and represents a nil binding as such.

https://github.com/ThesaurusSoftware/TFDatePicker

Run the TFDatePickerTest target to see how it performs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top