bindings with custom fieldeditor problem

The NSWindow delegate responsible for creating the customfield editor looks like this


-(id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject

{

// CustomTextView is a NSTextView subclass

return [[CustomTextView alloc]init];

}


)


The window has two NSEditFields (name & address) and an arraycontroller containing objects with two string properties (name & address)


name.value is bound to arraycontroller.selection.name

address.value is bound to arraycontroller.selection.address


The poblem:


When an arraycontroler item is selected the correct values appear in "name" & "address"

Start editing field "name"

Clear the arraycontroller selection


field "address" displays "No selection" (correct)

field "name" no changes, field stays active (wrong)


Same problem when I use this code:


-(id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject

{

return [[NSTextView alloc]init];

}


So its not my custom class!


No problems when I set window.delegate to nil.


It seems that the default behavior does some extra magic when bindings are involved.


Question: what is the correct way to create a custom fieldeditor that works correctly with bindings?

Answered by QuinceyMorris in 232047022

Did you read the documentation?


developer.apple.com/library/content/documentation/TextFonts/Conceptual/CocoaTextArchitecture/TextEditing/TextEditing.html


(See "Working with the Field Editor")


The sample code does two things you're missing:


1. It sets the "fieldEditor" property to YES.


2. It creates one field editor and caches it.


My guess is that #1 is causing the problem you see, but you should also do #2 because creating a new text view every time is expensive.

Accepted Answer

Did you read the documentation?


developer.apple.com/library/content/documentation/TextFonts/Conceptual/CocoaTextArchitecture/TextEditing/TextEditing.html


(See "Working with the Field Editor")


The sample code does two things you're missing:


1. It sets the "fieldEditor" property to YES.


2. It creates one field editor and caches it.


My guess is that #1 is causing the problem you see, but you should also do #2 because creating a new text view every time is expensive.

"The sample code does two things you're missing:"


Boy did I feel stupid after reading that comment 😟

I did read the documentation but for some reason I missed those 2 critical lines of code.


"1. It sets the "fieldEditor" property to YES.

2. It creates one field editor and caches it"


As it seems both are required!


It now works as expected

bindings with custom fieldeditor problem
 
 
Q