添加和删除视图时使用 Tab 键切换到字段时出现问题
我有一个包含 NSSearchField 的范围栏。可以使用菜单项显示和隐藏该栏。我通过创建一个新的 NSViewController(并从 XIB 加载一个新视图)来生成这个栏。当显示栏时,我对窗口的 contentView 执行 addSubview:;当隐藏栏时,我对视图控制器中的视图执行removeFromSuperview。
如果当我启动应用程序并且该栏已打开时,点击选项卡会在窗口内的主视图(表格视图)和范围栏中的搜索字段之间切换。如果我启动应用程序并且该栏尚未显示,那么一旦显示该栏,我可以从表格视图切换到搜索字段,但不能以其他方式切换。
一旦我第一次删除范围栏,然后再次显示它,无论当前选择哪个视图,我都无法再在搜索字段和表视图之间进行切换。
除了 addSubview: 和 removeFromSuperview 之外,我还需要做些什么吗?我无法理解为什么这不起作用,特别是为什么如果该栏在启动时显示或不显示,我会得到不同的行为。
I have an scope bar containing a NSSearchField. The bar can be shown and hidden using a menu item. I generate this bar by creating a new NSViewController (and loading a new view from the XIB). When the bar is shown, I do a addSubview: to the window's contentView; when the bar is hidden, I do removeFromSuperview to the view within the view controller.
If when I launch the app and the bar is already opened, hitting tab toggles between the main view within the window (a table view) and the search field in the scope bar. If I launch the app and the bar isn't already shown, once I do show the bar I can tab from the table view to the search field, but not the other way.
Once I remove the scope bar for the first time, then show it again, I can no longer tab between the search field and the table view, no matter which view is currently selected.
Is there something I need to be doing besides addSubview: and removeFromSuperview? I can't wrap my head around why this won't work, and especially why I get different behaviors if the bar is shown on launch or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想控制按 Tab 键时发生的情况,则需要设置两个视图的
nextKeyView
。但是,您需要小心,因为您可能会破坏自动构建的关键视图循环。 本文详细介绍了如何处理这种情况。
You need to set the
nextKeyView
of both views if you want to control what happens when you hit the tab key.However, you need to be careful because you can break the automatically constructed key-view loop. This article has more detail on how to handle this situation.
我能够通过在视图窗口上将 setAutorecalculatesKeyViewLoop: 设置为 true 来获得所需的行为。
I was able to get the desired behavior by setting setAutorecalculatesKeyViewLoop: to true on the views' window.