即使设置了可编辑行为,也无法在 NSPopover 上编辑 NSTextField
我有一个应用程序,它使用 NSTextField
打开弹出窗口。文本字段不可编辑。文本字段的行为设置为可编辑
。我仍然可以将文本粘贴和复制到此字段,但无法编辑它。
任何人都知道,可能出了什么问题?
I have an application, which open popover with NSTextField
. The text field is not editable. Behavior for text field is set to Editable
. I still can paste and copy text to this field but i can't edit it.
Anyone knows, what can be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不确定您是否仍然需要答案,但可能还有其他人仍在寻找答案。我在苹果开发者论坛上找到了解决方案。引用原作者的话:
主要问题是键盘事件的工作方式。尽管 NSTextField (及其所有超级视图)接收键盘事件,但它不会执行任何操作。发生这种情况是因为附加弹出窗口的视图位于无法成为关键窗口的窗口中。你无法以任何方式访问该窗口,至少我不能。因此,解决方案是使用类别覆盖应用程序中每个 NSWindow 的 canBecomeKeyWindow 方法。
这使得弹出窗口完全响应。如果您需要另一个必须对 canBecomeKeyWindow 响应“否”的窗口,您始终可以创建一个子类。
Not sure if you still need the answer, but there may be some others still looking. I found a solution on apple developer forums. Quoting the original author:
The main problem is the way keyboard events works. Although the NSTextField (and all its superviews) receives keyboard events, it doesn't make any action. That happens because the view where the popover is atached, is in a window which can't become a key window. You can't access that window in any way, at least I couldn't. So the solution is override the method canBecomeKeyWindow for every NSWindow in our application using a category.
That makes the popover fully resposive. If you need another window which must respond NO to canBecomeKeyWindow, you can always make a subclass.
我也为此苦苦挣扎了一段时间,直到我意识到这是一个错误。
但是,我发现使用已实现的 NSPopover 的 isShown 属性更可靠,而不是依赖 NSStatusItem 视图的 isActive 状态。
在我的代码中,我在 NSViewController 中有一个 NSPopover:
I struggled with this for a while as well, until I realized it was a bug.
However, instead of relying on an isActive state of a NSStatusItem view, I find it much more reliable to use the isShown property of the NSPopover you have implemented.
In my code, I have a NSPopover in a NSViewController:
Balazs Toth 的答案有效,但如果您将弹出窗口附加到 NSStatusItem.view,状态项将变得无响应 - 需要单击两次才能聚焦。
Balazs Toth's answer works, but if you're attaching the popover to NSStatusItem.view the status item becomes unresponsive - requiring two clicks to focus.
我在使用此解决方案时发现,当 NSStatusItem 变得无响应时,您可以轻松地覆盖此行为,如下所示
您将检查窗口的类,如果它与 NSStatusBarWindow 匹配然后我们可以以某种方式检查 NSStatusItem 是否处于活动状态。如果是,则意味着我们必须返回 YES,因为这样 NSStatusItem 中的 NSPopover 将拥有所有键盘事件。
我用来检查 NSStatusItem 是否被单击(或处于活动状态)的是,在我自己的自定义视图中,我有一个布尔值,当用户单击 NSStatusItem 时,该值会发生变化>,系统自动检查“canBecomeKeyWindow”,当它检查时,它将返回NO,并且在用户点击它之后(当它返回NO时>)它将改变bool 值,并在系统再次询问时返回 YES(当点击 NSPopover 进行 NSTextField 编辑时)。
旁注:
What i found when working with this solution is that when NSStatusItem becomes unresponsive, you can easily override this behavior like this
You will check for the class of the window, if it matches the NSStatusBarWindow we can then check somehow if the NSStatusItem is active. If it is, that means we have to return YES, because this way the NSPopover from NSStatusItem will have all keyboard events.
What I'm using for checking if the NSStatusItem was clicked (or is active) is that in my own custom view i have a bool value which changes when user clicks on the NSStatusItem, system automatically checks for "canBecomeKeyWindow" and when it does it will return NO and after user clicks on it (while it is returning the NO) it will change the bool value and return YES when system asks again (when NSPopover is being clicked for NSTextField editing).
Sidenotes:
如果有人仍在寻找这个问题的答案,我正在 Swift 中工作。
当您希望字段允许文本输入时,我使用
myTextField.becomeFirstReponder()
来选择退出;只需使用 myTextField.resignFirstResponder() 即可
If anyone is still looking for an answer to this, I am working in Swift.
At the time where you wish the field to allow text entry, I have used
myTextField.becomeFirstReponder()
To opt out; just use
myTextField.resignFirstResponder()
绝对是一个错误。该错误报告正是我想要做的。甚至到创建状态项和覆盖鼠标按下。
我可以确认巴拉兹·托特的答案有效。我只是想知道它是否会妨碍我的前进。
Definitely a bug. That bug report is exactly what I was trying to do. Even down to creating the status item and overriding mousdown.
I can confirm that Balazs Toth's answer works. I just wonder if it might get in the way down the road.
如果有人得到它并且上面的解决方案对他不起作用。
我的应用程序中的问题出在我的应用程序设置的
targets
中的info
选项卡中,并且应该
在这件事上花费一整天。
If someone gets it and the solution above didn't do the trick for him.
The problem in my app was in the
info
tab in thetargets
my application was set toand shulde of been
Spent an entire day on this thing.
漏洞。 http://openradar.appspot.com/9722231
Bug. http://openradar.appspot.com/9722231