Re: Possible to bind file contents to a view?
Re: Possible to bind file contents to a view?
- Subject: Re: Possible to bind file contents to a view?
- From: mmalcolm crawford <email@hidden>
- Date: Mon, 28 Mar 2005 01:17:35 -0800
On Mar 28, 2005, at 1:03 AM, Julian Dreißig wrote:
Am 28.03.2005 um 02:06 schrieb mmalcolm crawford:
On Mar 27, 2005, at 11:21 AM, Julian Dreißig wrote:
is it possible to bind the contents of a (text) file to a
NSTextView? I would like to have a window which reflects the
current contents of a text file (a log file).
You're thinking about bindings backwards. You bind an attribute
of a view to a controller or model object. This misconception
leads yo astray here:
Sorry, why is it wrong what I do? The reference tells me that
binding is supposed to be "automatically synchronizing views when
models change".
Yes, but you bind an attribute of a view to a property of a
controller or model object, you don't bind a model property to a view.
I also tried to load the file with a NSFileWrapper and -
initWithPath: and displayed its contents using -
regularFileContents. When I do some changes to the text file and
then try to update with -updateFromPath:, -regularFileContents
does not reflect changes made to the file. I have to release the
wrapper and create a new one.
regularFileContents is not KVO compliant (certainly insofar as I
can tell, and there's nothing in the documentation to suggest it is).
I observed this behaviour even without bindings, as the above
example shows.
It will work once, when you first establish the binding. Thereafter
it won't be updated. The update is propagated using KVO -- see, for
example, <http://developer.apple.com/documentation/Cocoa/Conceptual/
CocoaBindings/Concepts/HowDoBindingsWork.html>
Bind to something that is KVO compliant.
This may be a stupid question, but will it work if I wrap the
NSFileWrapper into a self-defined KVO compliant object?
I presume that you have an intermediate controller object that
currently returns the fie wrapper or its data? Write an accessor
method to return the file wrapper's data and bind to that.
How does the controller get notified about changes to the file?
You will have to use another mechanism to watch for updates to the
file (whether you use something like kqueue or use a timer and
needsToBeUpdatedFromPath:) and then post KVO notifications
appropriately. The tricky part may be posting old and new values
unless you cache the data locally (although you may be able just to
retrieve the old value directly from the text view itself).
mmalc
_______________________________________________
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