I am facing a problem that after hours of searching, I cannot find a solution to.
The situation is like this: I have multiple NSTextFields whose contents are binded to various key path of an NSArrayController displayed in a NSTableView (done in IB).
I added an arithmetic function to the fields. Say, for example, you enter 2+2, then it will be converted to 4. But for some strange reason even though I see the value being updated correctly in the NSTextField, the corresponding element in the NSArrayController’s content is set to 2+2, which, obviously, is not what I want.
I am using controlTextDidEndEditing_(aNotification) to capture the end of the editing, then using aNotification’s object()'s stringValue() to get the contents (which is 2+2), then do the math and update the content of the NSTextField to the final calculation with aNotification’s object()'s setStringValue_(calcResult).
I have looked a Notifications from the NSTextField (and NSCell, and NSControl), nothing that can help me there, same for NSArrayController.
Ideally, I would need to have a link to the exact content of the binded NSArray (via the NSArrayController) based on the object that sent the notification… is there a way to get this information?
Thanks a lot!
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)
Yeah, thought it might be difficult to understand as I was having problems explaining this. Thanks for replying…
I have a NSTableView whose content is binded to an NSArray (controlled of course by an NSArrayController). As I have way too much information to edit for each row, I opted instead to let the user use multiple NSTextFields to edit the contents of the currently selected row in the table view.
Some of them are to specify the dimensions of a template, and to help the user in entering the values I added a method that captures the content of the text field once the edit is done and check if it has a mathematical calculation in it, and if there is one, perform the operation and return the result in the text field. I have dozens of these fields and I want to use the same method to do the calculations, as I want a uniform UI, and I also plan to improve this method considerably in the future.
All of this works perfectly. But the problem is although the value is sent back to the NSTextField correctly, the corresponding value in the selected row in the NSTableView is not, instead the mathematical calculation is stored.
For example If I enter 2+2, the NSTextField changes to 4, but the NSArray is sticking with 2+2.
I’d like to know if there is a way to send back the result in the NSArray displayed in the NSTableView using the notification’s object. Any ideas?
Thanks!
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)
If I understood correctly what DJ said in this other post http://macscripter.net/viewtopic.php?id=36453, the NSTextField and the item into the NSArray are two different objects created by Cocoa, even if they seem be the same object from Applescript’s point of view. Maybe you have to update the value programmatically.
It’s still a little hard to understand what is going on without seeing the code, but I have set up table views where the user adds to the table by filling in text fields – one thing I’ve found necessary after saying "theData’s addObject_ " or “theData’s addObjects_” is to add the line setTheData_(theData) to make everything update properly (where theData is my array that is bound to the array controller’s content).
Thank you for your reply. I’d like to post the code, but it’s spread across a few classes… but the only thing I did that is special is the math method, the rest is simple bindings done in IB.
My problem isnt really that the data is not updated in the NSArray, it’s just that the value entered is stored instead of the result I do in a separate method.
What I’d like is a method or framework that would let me update the data with the proper value inside the binded NSArray. Like a method that forces the update of the currently selected row in the array based on the contents of the binded NSTextFields…
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)
Alright, got somewhat of a fix. I used unique tag numbers on each text fields and process them based on that number to find out which is which in relation to the NSArray. Not perfect, adds 100 lines of code, but it works.
Still, if anyone has a better suggestion, i’m open to improvement!
Thanks for your suggestions everyone.
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)
I did the following which worked for a test app that I made. One column of my table was bound to Array Controller.arrangedObjects.num and the text field for editing that column was bound to Array Controller.selection.num My script was set as the delegate for that text field and I put the following method in my script:
on ControlTextDidEndEditing_(aNote)
set obj to aNote's object()
-- pretend I entered 2+4 and did the math to get 6
obj's setStringValue_("6")
controller's setValue_forKeyPath_(obj's stringValue(), "selection.num")
end ControlTextDidEndEditing_