CoreData-bound NSTableView loses input focus when

2019-04-13 03:53发布

问题:

I have an NSTableView in a dialogue box which is bound to a collection of CoreData model instances via an NSArrayController in 'Entity Name' mode. The table displays the names of the array of managed objects in a single column. This works well.

The names in the table rows are editable. If the user edits a name when there is no sorting applied to the table then editing proceeds normally. After pressing Return, the new name is recorded and input focus stays in the NSTableView. However, if the column heading in the table view is clicked upon in order to sort the table of names, input focus goes astray after editing. It stays within the window, or wider view (not sure which), but it goes out of the table; the focus ring vanishes and the background colour of the highlighted item changes from blue to grey. Pressing Tab pops input focus back into the table view again.

This only happens if the table contents are sorted. If "Continuously Updates Value" is chosen for the binding, it's catastrophic as the minute any characters are entered, the table view seems to want to re-sort itself (that's OK) and focus jumps out of it (that's not OK as the user was in the middle of trying to type something).

As far as this aspect of the system is concerned, there's no code - it's all done with bindings established in Interface Builder. Presumably, I've inadvertently set or cleared some option that I shouldn't have.

In case it helps the reader figure out what's up - I also have a modal sheet attached to the dialogue box containing the NSTableView. The sheet is used to edit the details of an item selected in the table view. The controls in this are also connected with bindings to the CoreData model using the same NSArrayController as the dialogue box 'behind' the sheet. The same problem is seen - as soon as a new name is typed in, focus is pulled back to the dialogue box 'behind' the modal sheet.

The only code involved is that used to handle the 'edit this item' action and start the modal session for the sheet.

What's going on? Where is the focus going and why is being moved just because of re-sorting in the NSTableView?

Thanks!

回答1:

The entities NSArrayController had "Auto Rearrange Content" ticked in the relevant Interface Builder inspector panel. This wasn't doing what I thought it would do and was the cause of the focus stealing problem.

I finally narrowed this down by creating a bare bones CoreData application which just added names to a table view. Almost no code required; 99.5% Interface Builder and bindings, with just an extra outlet and a single line of glue code to tell the array controller for the CoreData model about the Managed Object Context instantiated in the application delegate by code that Interface Builder had auto-generated. Setting the "Auto Rearrange Content" flag in the test program provoked the same strange input focus behaviour.

So if you've got focus stealing problems with a table of objects bound to CoreData through an array controller, check your array controller's auto-rearrange flag!