1

我只有一个主从类型的应用程序 OS X 10.9:
窗口左侧的 NSTableView,右侧的详细信息为 NSTextFields。
我没有专门设置标签顺序,开箱即用就可以了。

插入新对象后除外:

  • 插入新对象后,该对象在 NSTableView 中被选中。
  • 点击标签
  • 第一个 NSTextField 被选中
  • 写点东西,点击标签
  • 现在没有选择下一个 NSTextField 而不是选择任何内容,
  • 点击标签
  • NSTableView 被选中
  • 点击标签
  • 第一个 NSTextField 被选中

我想要的是:

  • 插入新对象后,应该选择detail部分的第一个NSTextField
  • 点击标签
  • 应该选择详细部分的下一个 NSTextField
  • …</li>

更新:我发现奇怪的行为是通过在 Interface Builder 中启用 NSArrayController 的“自动重新排列内容”来触发的。如果我禁用它,我不会再失去焦点。请参阅CoreData-bound NSTableView 在项目更改时失去输入焦点,但前提是排序

但是,当然,我仍然希望我的内容能够自动排序——只是在操作过程中不会失去我的注意力。这可以做到吗?

UPDATE2:当我的文本字段失去键盘焦点时,我遵循了@KenThomases 的建议并记录了堆栈跟踪:

2014-08-17 16:30:26.003 ResponderExperiment[712:303] (
    0   ResponderExperiment                 0x00000001000014dd -[WMWindow makeFirstResponder:] + 61
    1   AppKit                              0x00007fff8d25c778 -[NSTextView(NSPrivate) _giveUpFirstResponder:] + 257
    2   AppKit                              0x00007fff8d25c56c -[NSTextView(NSKeyBindingCommands) insertTab:] + 270
    3   AppKit                              0x00007fff8d22dc2f -[NSResponder doCommandBySelector:] + 71
    4   AppKit                              0x00007fff8d25b10a -[NSTextView doCommandBySelector:] + 196
    5   AppKit                              0x00007fff8d22d151 -[NSKeyBindingManager(NSKeyBindingManager_MultiClients) interpretEventAsCommand:forClient:] + 1392
    6   AppKit                              0x00007fff8d24c1c2 -[NSTextInputContext handleEvent:] + 845
    7   AppKit                              0x00007fff8d22b9dd -[NSView interpretKeyEvents:] + 180
    8   AppKit                              0x00007fff8d24bd6d -[NSTextView keyDown:] + 658
    9   AppKit                              0x00007fff8d1f856b -[NSWindow sendEvent:] + 1843
    10  AppKit                              0x00007fff8d199b32 -[NSApplication sendEvent:] + 3395
    11  AppKit                              0x00007fff8cfe99f9 -[NSApplication run] + 646
    12  AppKit                              0x00007fff8cfd4783 NSApplicationMain + 940
    13  ResponderExperiment                 0x0000000100002de2 main + 34
    14  libdyld.dylib                       0x00007fff8c5bf5fd start + 1
    15  ???                                 0x0000000000000003 0x0 + 3
)
2014-08-17 16:30:26.014 ResponderExperiment[712:303] (
    0   ResponderExperiment                 0x00000001000014dd -[WMWindow makeFirstResponder:] + 61
    1   AppKit                              0x00007fff8d03c918 -[NSControl abortEditing] + 83
    2   AppKit                              0x00007fff8d2723e0 -[NSValueBinder discardEditing] + 162
    3   AppKit                              0x00007fff8d1aee30 -[NSController discardEditing] + 115
    4   AppKit                              0x00007fff8d1af971 -[NSArrayController rearrangeObjects] + 27
    5   AppKit                              0x00007fff8d3697a9 -[NSArrayController observeValueForKeyPath:ofObject:change:context:] + 294
    6   AppKit                              0x00007fff8d39ac08 -[NSArrayController _setMultipleValue:forKeyPath:atIndex:] + 323
    7   AppKit                              0x00007fff8d39ba12 -[NSArrayController _setSingleValue:forKeyPath:] + 137
    8   Foundation                          0x00007fff8bf9e19a -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 285
    9   AppKit                              0x00007fff8d25444c -[NSBinder _setValue:forKeyPath:ofObject:mode:validateImmediately:raisesForNotApplicableKeys:error:] + 364
    10  AppKit                              0x00007fff8d254287 -[NSBinder setValue:forBinding:error:] + 245
    11  AppKit                              0x00007fff8d74c4ee -[NSValueBinder _applyObjectValue:forBinding:canRecoverFromErrors:handleErrors:typeOfAlert:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:] + 194
    12  AppKit                              0x00007fff8d74c871 -[NSValueBinder applyDisplayedValueHandleErrors:typeOfAlert:canRecoverFromErrors:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:error:] + 621
    13  AppKit                              0x00007fff8d74c9d4 -[NSValueBinder _applyDisplayedValueIfHasUncommittedChangesWithHandleErrors:typeOfAlert:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:error:] + 127
    14  AppKit                              0x00007fff8d253c2e -[NSValueBinder validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:] + 436
    15  AppKit                              0x00007fff8d253a57 -[_NSBindingAdaptor _validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:bindingAdaptor:] + 160
    16  AppKit                              0x00007fff8d25399d -[_NSBindingAdaptor validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:] + 260
    17  AppKit                              0x00007fff8d25e0ba -[NSTextField textShouldEndEditing:] + 402
    18  AppKit                              0x00007fff8d25dc63 -[NSTextView(NSSharing) resignFirstResponder] + 393
    19  AppKit                              0x00007fff8d136170 -[NSWindow makeFirstResponder:] + 455
    20  ResponderExperiment                 0x000000010000154c -[WMWindow makeFirstResponder:] + 172
    21  AppKit                              0x00007fff8d25c778 -[NSTextView(NSPrivate) _giveUpFirstResponder:] + 257
    22  AppKit                              0x00007fff8d25c56c -[NSTextView(NSKeyBindingCommands) insertTab:] + 270
    23  AppKit                              0x00007fff8d22dc2f -[NSResponder doCommandBySelector:] + 71
    24  AppKit                              0x00007fff8d25b10a -[NSTextView doCommandBySelector:] + 196
    25  AppKit                              0x00007fff8d22d151 -[NSKeyBindingManager(NSKeyBindingManager_MultiClients) interpretEventAsCommand:forClient:] + 1392
    26  AppKit                              0x00007fff8d24c1c2 -[NSTextInputContext handleEvent:] + 845
    27  AppKit                              0x00007fff8d22b9dd -[NSView interpretKeyEvents:] + 180
    28  AppKit                              0x00007fff8d24bd6d -[NSTextView keyDown:] + 658
    29  AppKit                              0x00007fff8d1f856b -[NSWindow sendEvent:] + 1843
    30  AppKit                              0x00007fff8d199b32 -[NSApplication sendEvent:] + 3395
    31  AppKit                              0x00007fff8cfe99f9 -[NSApplication run] + 646
    32  AppKit                              0x00007fff8cfd4783 NSApplicationMain + 940
    33  ResponderExperiment                 0x0000000100002de2 main + 34
    34  libdyld.dylib                       0x00007fff8c5bf5fd start + 1
    35  ???                                 0x0000000000000003 0x0 + 3
)

所以也许我可以编写自己的 NSArrayController rerangeObjects: 方法,这样不会失去焦点?

4

2 回答 2

2

这是一个可能的解决方案。这个想法是将文本字段(或更准确地说,为文本字段提供服务的字段编辑器)对 Tab 键的响应重新实现为两个单独的步骤。首先,结束编辑,这将使阵列控制器重新排列其内容。我们通过重置焦点来结束编辑,所以我们不管阵列控制器可能会发生什么来聚焦。然后,我们自己从最初聚焦的文本字段中推进焦点。

在您的文本字段上设置一个委托。在该委托中,实现以下方法:

- (BOOL) control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)command
{
    if (command == @selector(insertTab:))
    {
        // End editing. This will commit the edited value and cause the array controller
        // to rearranged its contents.
        [control.window makeFirstResponder:nil];

        // This will put focus on the next control from the text field.
        [control.window selectKeyViewFollowingView:control];

        // Prevent the normal processing of this command
        return YES;
    }

    if (command == @selector(insertBacktab:))
    {
        // End editing. This will commit the edited value and cause the array controller
        // to rearranged its contents.
        [control.window makeFirstResponder:nil];

        // This will put focus on the previous control from the text field.
        [control.window selectKeyViewPrecedingView:control];

        // Prevent the normal processing of this command
        return YES;
    }

    return NO;
}

我不确定的一件事是:如果您单击另一个文本字段而不是远离文本字段,会发生什么情况。在这种情况下,阵列控制器是否仍会重置焦点?

于 2014-08-17T19:26:20.787 回答
0

就我而言,我已经用我自己的逻辑覆盖了 NSControl 的 abortEditing 方法。

- (BOOL)abortEditing {
    MyObject *object = [myArrayController.arrangedObjects objectAtIndex:self.editedRow];
    return ![[Manager sharedInstance] isObjectBeingEditing:object];
}
于 2017-01-09T11:09:28.023 回答