Can you Bind to the timeInterval attribute of an NSDatePicker? - objective-c

I've got a Core Data application that has an Event class, which has a start date and a finish date. It's trivial to bind these to a pair of NSDatePicker widgets, but I wanted to make it work with the NSRangeDateMode available in Leopard.
The NSDatePicker has a pair of methods that deal with timeInterval, but I don't seem to be able to bind to this.
Update: I've used a manual call to do the binding, and it half works:
[picker bind:#"timeInterval"
toObject:array
withKeyPath:#"selection.timeInterval"
options:options];
It sets the timeInterval in the NSDatePicker when the underlying object is changed, but does not set the underlying object when the NSDatePicker's timeInterval is changed.

Sadly, no. The timeInterval property of the date picker is not even properly key-value observable. Basically, you're stuck either setting up an action method or using the delegate validation method to receive updates to its value. Also, you'll want to round it off to the nearest multiple of 86400.0 (i.e. the number of seconds in a day), since the date picker is consistently off by some fraction of a second in its reported timeInterval. Perhaps by the time Snow Leopard rolls around, this feature will be fully baked.

The interval support is only available when you're using the graphical version of the date picker. Even then, there's no native binding support for timeInterval.
Also depending on how you're intending to use this the UI to select ranges that extend past the current month is poor in my opinion.

1169097 explains how to implement custom bindings.

Related

NSDatePicker bindings

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:
Initial value. Date picker shows it's initial date (set in IB) but bound property returns nil (if user does not interact with picker).
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.
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.
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.

NSTextField doubleValue localization confusion

I am from germany. So I have a little problem with localization and the property doubleValue of the class NSTextField. Comma and points are different from the U.S.
double a = self.heightTextField.doubleValue;
If I have a "11,11" in my textfield I only get the integer part unless I write "11.11" with a point. So I guess I have to convert this value according to my current location. No big deal. NSNumberFormatter will do that.
But if I assign a value to the property it all works fine automatically. "11.11" gets converted into "11,11" on the NSTextField without me doing anything.
self.heightTextField.doubleValue = 11.11;
Why is that so?
Why is the the value from the controller converted automatically when send to the view but when I read a value from the view I have to convert it myself? Can I get automatic conversion both ways?
The number handling in NSTextField is from the old, old days, and is being gently deprecated in favor of NSFormatters, so it’s not surprising it’s kind of spotty.
As #Michael says, if you want to handle different localizations of you should add an NSNumberFormatter to your field. You can just drag one off of the Interface Builder’s classes palette and onto the field in question.

Checking previous created NSManagedObject

I have an NSManagedObject-subclass Event - with some event data in user's life, also it have an NSNumber-type field called time, with unix timestamp representation of when this event had place. So typically user will have many Events. In showing current Event data through tableView I also need to show some data from previous by date Event. Does Core Data have some marks when objects created, that I can directly know what exactly Event was created before current, or do I need to fetch with predicate by value of time?
You should fetch objects from CoreData, and if you're using UITableView it's a good practice to use NSFetchedResultsController, which has a lot of good features for presentation data in UITableView

How would you go about implementing a "dayDidChange" method, while avoiding the use of NSTimer?

How would you go about implementing a "dayDidChange" method?
I have a dateLabel, that needs updating, when the day changes.
I already had 2 solutions implemented. But gave up on them.
1st, on viewWillAppear, I would set my label. Chances for date changing when the user is viewing the VC are VERY small ... and it wouldn't really introduce any "errors" to execution But still, it's not perfect.
2nd, I also implemented an NSTimer with intervals of 1 second. Works great. But I'm not too found of the idea of having a method being called if there is a chance that I don't need to do it.
Are there any other options? I need to update that label, when the day changes. Also, is it possible to use NSNotificationCenter with this?
In your appliciation delegate, implement the following method: -(void)applicationSignificantTimeChange:(UIApplication *)application
This method is called when the day changes, or if the device's time has been changed in the background for whatever reason (such as changes to time zone).
hope it helps. happy coding :)
You can calculate the time left on the day and set a timer to it. You can also listen to UIApplicationSignificantTimeChangeNotification. Note that this notification will be fired not only when the date changes, but for timezone adjustments and other changes. Keep in mind also that the firing of the notification will be triggered only if your app is not in an inactive state.
There are many ways to do so, and I can elaborate more on them if needed.
But here is a nice quick trick
use this code in your app delegate
it will listen to time changes (when the date or time changes dramatically) and when that happens - run a test and update the label if needed.
read me about this here: https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html
It's just a quick simple way of doing it without timers.
- (void)applicationSignificantTimeChange:(UIApplication *)application {
NSLog(#"Time has changed, do a test and update the label if needed");
// [yourViewController runTimeTestAndUpdateFunction];
}

Changing a view's initialization based on properties

I am trying to build an approach to initialize a view dynamically with different components. Hence, I need to do this on the fly at initialization time. I was thinking of having a struct with default values, and if at any given time the BOOL property of enabling changes, I can reload the input views.
Hence I can loop through the BOOL properties and check if something have changed. Is there a better way of doing this, or does that sound about right?
The architecture:
UIView -> Buttons 1 to 10 laid equally in distance. Based on users preference, the button can be turned on/off, however by default they are all on.
You can use key value coding observer to identify when a value has changed. This could be your trigger to do the necessary update. Polling a boolean value for changes is an unnecessary step and might cause your app to become unresponsive. Also, evaluate if the action that causes the necessity to update/change the view is an action, then you can just implement the changes inside the proper action. For more information, check the Key-Value Observing Programming Guide : http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html