Re: Binding Text Field to Show Time Interval?
Re: Binding Text Field to Show Time Interval?
- Subject: Re: Binding Text Field to Show Time Interval?
- From: Ondra Cada <email@hidden>
- Date: Fri, 28 Apr 2006 20:17:55 +0200
Christopher,
On 28.4.2006, at 20:00, Christopher Hickman wrote:
This seems like something you should be able to do with bindings,
but I'm
not sure. I have a text field that I want to show the time
interval in
seconds since a reference date (set programmatically). Is there a
way to
bind the field to automatically update itself to show this?
Well, first you need an attribute somewhere in the model or
controller to which you can bind: can be an instance variable, but in
this case you'd rather want only a method. The attribute must update
KVO-savvy (i.e., it must be changed through setValue:forKey:, or
through acccessor, or will/didChangeValueForKey: must be called).
Then you just bind the textfield (any number of them :)) to that, and
it is done: it will show the interval in seconds automatically.
An example implementation (written to Mail directly, typos possible,
errors probable :)) could be something like
-(NSTimeInterval)timeInterval { // you bind to this attribute
return -[referenceDate timeIntervalSinceNow];
}
-(void)ensureTimeIntervalUpdate { // you call this from init or whatever
[self willChangeValueForKey:@"timeInterval"];
[self didChangeValueForKey:@"timeInterval"];
[self performSelector:_cmd withObject:nil afterDelay:.5]; // or
whatever granularity you want
}
If you wanted a more elaborate display (like, say, days/hours/minutes/
seconds), you need also a formatter on the field or a value
transformer on the binding, but that, I guess, is at this moment
irrelevant.
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden