gpt4 book ai didi

cocoa - 当启用 NSArrayController“自动重新排列内容”时,插入和编辑新对象后键盘焦点丢失

转载 作者:行者123 更新时间:2023-12-03 17:02:44 24 4
gpt4 key购买 nike

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

插入新对象后除外:

  • 插入新对象后,该对象将在 NSTableView 中被选中。
  • 点击标签
  • 第一个 NSTextField 被选中
  • 写点什么,点击 Tab
  • 现在不是选择下一个 NSTextField 什么都没有被选择,
  • 点击标签
  • 选择了 NSTableView
  • 点击标签
  • 第一个 NSTextField 被选中

我想要什么:

  • 插入新对象后,应选择详细信息部分的第一个 NSTextField
  • 点击标签
  • 应选择详细信息部分的下一个 NSTextField
  • ...

更新:我发现奇怪的行为是通过在 Interface Builder 中启用 NSArrayController 的“自动重新排列内容”来触发的。如果我禁用它,我就不会再失去焦点。请参阅 CoreData-bound NSTableView loses input focus when items change, but only if sorted

但是,当然,我仍然希望我的内容能够自动排序 - 只是在操作期间不会失去焦点。这可以吗?

更新2:当我的文本字段失去键盘焦点时,我遵循了@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
)

那么也许我可以编写自己的 NSArrayControllerrearrangeObjects: 方法,该方法不会失去焦点?

最佳答案

这是一个可能的解决方案。这个想法是将文本字段(或者更准确地说,为文本字段提供服务的字段编辑器)对 Tab 键的响应重新实现为两个单独的步骤。首先,结束编辑,这将使阵列 Controller 重新排列其内容。我们通过重置焦点来结束编辑,因此我们不关心阵列 Controller 可能会做什么来获得焦点。然后,我们自己将焦点从最初聚焦的文本字段移开。

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

- (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;
}

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

关于cocoa - 当启用 NSArrayController“自动重新排列内容”时,插入和编辑新对象后键盘焦点丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25311016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com