首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态更新NSTouchBar,以编程方式添加/删除项

动态更新NSTouchBar,以编程方式添加/删除项
EN

Stack Overflow用户
提问于 2016-11-23 08:59:14
回答 2查看 2.7K关注 0票数 7

我目前正在为我的NSTouchBar应用程序实现macOS api。

此时,我所拥有的唯一的触摸条有主视图控制器作为它的委托,并且我可以很好地向它添加项目。问题是,只有在满足某个条件(在表中选择了一行)时,我才需要这些项中的一些。

假设我有一个布尔值,该布尔值指示按钮是否应该可见。如何动态更新NSTouchBar以显示/隐藏此按钮,以防我的布尔值发生变化?(我不需要观察这个布尔值,我只需在我已经实现的另一个方法中调用更新)

现在我所做的是:在touchBar(:makeItemForIdentifier)中,我对所有标识符都有一个开关,在正确的case下,我要么返回带有按钮的NSCustomTouchBarItem,要么返回nil (如果我的布尔值是false )。

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

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-23 09:11:11

四种想法:

  1. 尝试将触摸栏的defaultItemIdentifiers更改为应该显示的项标识符集。请注意,如果用户定制了触摸条,这将是个问题,但我认为按需交换项目和自定义触摸条并不能很好地结合在一起。这也有一个优点,即您不需要在touchBar(:makeItemForIdentifier:)中返回零。
  2. 调用makeTouchBar()将创建一个新的NSTouchBar实例,但不会更改touchBar属性。试着做些像 viewController.touchBar = viewController.makeTouchBar() 或 viewController.touchBar =零
代码语言:javascript
复制
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).
2. 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`).

票数 10
EN

Stack Overflow用户

发布于 2016-11-28 08:16:12

只需使视图控制器中的接触条失效:

self.touchbar = nil

然后将自动调用委托方法makeTouchBar()。使用您的标志,您可以很容易地选择图标显示。

编辑:这个解决方案已经过测试并运行良好。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40759909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档