快速更新NSTouchBar以编程方式添加/删除项目 [英] Update NSTouchBar on the fly to add/remove items programmatically

查看:153
本文介绍了快速更新NSTouchBar以编程方式添加/删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在对macOS应用程序实施NSTouchBar api.

I'm currently implementing the NSTouchBar api to my macOS application.

这时,我拥有的唯一触摸条将主View Controller作为其委托,并且可以向其中添加项目.问题是,我需要其中某些项仅在满足特定条件(在表中选择一行)时才显示.

At this moment, the only touch bar I have has the main View Controller as its delegate and I can add items to it fine. The catch is, I need some of those items to appear only when a certain condition is met (a row is selected in a table).

考虑一下,我有一个布尔值,指示按钮是否应该可见.我如何动态更新NSTouchBar以显示/隐藏此按钮,以防我的布尔值发生变化? (我不需要观察这个布尔值,我可以简单地通过已实现的另一种方法进行调用来更新)

Consider I have a boolean that indicates whether or not the button should be visible. How do I update the NSTouchBar on the fly to show/hide this button in case my boolean changes? (I don't need to observe this boolean, I could simply make the call to update in another method I already implemented)

我现在所做的是:在touchBar(:makeItemForIdentifier)中,我有一个用于所有标识符的开关,在正确的case下,我用按钮返回了NSCustomTouchBarItem,或者如果我的则返回了nil.布尔值是false.

What I did for now is the following: in touchBar(:makeItemForIdentifier), I have a switch for all identifiers, and under the proper case, I either return the NSCustomTouchBarItem with the button, or nil if my boolean is false.

在选择了表格的一行之后,我尝试再次调用makeTouchBar,但是它不会更新按钮的可见性,就像没有再次调用touchBar(:makeItemForIdentifier)一样.

I tried calling makeTouchBar again after a row of the table is selected but it doesn't update the buttons' visibility, as if touchBar(:makeItemForIdentifier) is not called again.

谢谢!

推荐答案

四个想法:

  1. 尝试将触摸栏的defaultItemIdentifiers更改为应显示的项目标识符集.请注意,如果用户自定义了触控栏,这将是有问题的,但是我认为按需交换项目和自定义触控栏还是不能很好地进行.这也具有不需要在touchBar(:makeItemForIdentifier:)中返回nil的优点.
  2. 调用makeTouchBar()将创建一个新的NSTouchBar实例,但不会更改touchBar属性.尝试类似

  1. Try changing your touch bar's defaultItemIdentifiers to the set of item identifiers that should be shown. Note that this would be problematic if the user has customized the touch bar, but I think swapping items on-demand and customizing the touch bar doesn't go well together anyway. This also has the advantage that you don't need to return nil in touchBar(:makeItemForIdentifier:).
  2. Calling makeTouchBar() will create a new NSTouchBar instance, but not change the touchBar property. Try something like

viewController.touchBar = viewController.makeTouchBar()

viewController.touchBar = nil

  1. NSTableRowView上设置touchBar属性,该属性在选中时应显示额外的项目,并确保在defaultItemIdentifiers中包括otherItemsProxy.由于触摸条的内容由响应者链中的所有元素组成,因此它可能包括表行的touchBar属性(假设它可以成为第一响应者).

  1. Set the touchBar property on the NSTableRowView that should show extra items when selected, and make sure to include the otherItemsProxy in your defaultItemIdentifiers. As the contents of the touch bar are comprised of all elements in the responder chain, this might include the touchBar property of the table row (provided it can become first responder).

您确定在未选择该行时是否应隐藏这些项目?考虑改为禁用它们(例如,将它们包含的按钮的enabled属性设置为false).

Are you sure that these items should be hidden when the row is not selected? Consider disabling them instead (e.g. by setting the enabled property of the buttons they contain to false).

这篇关于快速更新NSTouchBar以编程方式添加/删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆