Re: [SOLVED] Re: NSTextField setStringValue not updating properly
Re: [SOLVED] Re: NSTextField setStringValue not updating properly
- Subject: Re: [SOLVED] Re: NSTextField setStringValue not updating properly
- From: mmalc Crawford <email@hidden>
- Date: Thu, 11 Dec 2008 10:59:13 -0800
For the benefit of others with the same issue:
On Dec 10, 2008, at 8:31 AM, email@hidden wrote:
Anytime the value for the text field needs to change, I call
[NSTextField setStringValue:(NSString *)aString].
After searching through the message archives, I either find bound
fields or something else. The messages that address stuff close
this stipulate using the above call.
I have also tried validateEditing and display, both to no avail. I
can't seem to locate another viable method to get the data stored
(like a "refresh").
What appears to happen (in terms as best I can describe it, not
necessarily technically correct) is that the text is written to the
control's view, but, isn't saved to the underlying data structure.
Assuming that you're using bindings, the problem is described and
answered in the troubleshooting article here:
<http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaBindings/Concepts/Troubleshooting.html#//apple_ref/doc/uid/TP40002148-182809
>
"Changing the value in the user interface programmatically is not
reflected in the model
If you change the value of an item in the user interface
programmatically, for example sending an NSTextField a setStringValue:
message, the model is not updated with the new value.
This is the expected behavior. Instead you should change the model
object using a key-value-observing compliant manner."
If you're not using bindings, then the same general point applies --
simply updating a view does not cause the change to be propagated to
the underlying model object; you have to do that yourself.
On Dec 11, 2008, at 10:28 AM, email@hidden wrote:
What I ended up doing was implementing KVC and KVO for the variable
with which I was experiencing trouble. This is (at least) done by
creating a property, or by implementing the getter(s)/setter(s) w/
the appropriate key-value observing structure.
In general, you should not have to "implement KVC and KVO" for
properties, you should simply adhere to appropriate KVC naming
conventions and use the automatic KVO support you then get for free:
<http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/KeyValueCoding.html
>
<http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html
>
<http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/Concepts/AutoVsManual.html
>
I bound the textfield's value to the member of the class that was
used. That addressed the issue of keeping the textfield up-to-date
with the value of the variable as required.
I did, however, run into another issue: the run loop.
Coming from the VB world, I hadn't really needed any sort of
variables (extremely rarely, if ever) that "hung around." Most
everything I did there stuck by nature of the beast (depending upon
where it was declared).
Here, however, one has to deal with "the run loop." I may not have
a complete understanding of it, however, the part that matters for
this thread is that given a set of code it will only execute that
code once and only on the information it has at the time of
execution. Thus, if you need to pass data from one execution loop
to another (think callback), it needs to persist. That's done
either through the keyword static or, in the case of an NSObject of
some kind, retain.
This is so vague and misleading it's difficult to frame a response
beyond simply:
If you're using Cocoa, you should adhere to the basic memory
management rules as given in this document:
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
>
and summarised in this article:
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html
>
To understand where the run loop fits in to this, see:
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concepts/AutoreleasePools.html
>
In particular, if you need to maintain instance variables, don't
sprinkle your code with retain and release, but instead adhere to the
advice given here:
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html
>
see "Using Accessor Methods"
This is all fundamental, basic, required reading for any Cocoa
developer.
In summary, changing over to KVC w/ KVO plus the retention factor
fixed my issues. So, if you are having trouble with setStringValue,
make sure you are going the KVC w/ KVO route & check your bindings.
If you are having trouble with setStringValue:, and you're using
bindings, then per the Troubleshooting Guide you're almost certainly
doing the wrong thing. The answer is *not* to diddle with anything to
do with "KVC/KVO", but to instead Do the Right Thing: use the
technology the way it was designed -- change the model value, and
allow that to propagate to the UI.
mmalc
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden