gpt4 book ai didi

swift - 动态 NSTouchBar 项目

转载 作者:行者123 更新时间:2023-11-30 11:11:59 27 4
gpt4 key购买 nike

我想知道是否可以动态创建 NSTouchBar 项目。在我的应用程序中,我有一个数据源协议(protocol)来填充 NSTableViewController 的内容。我想使用此协议(protocol)也用相同的选项填充 touchBar。

这是我正在使用的协议(protocol)。

protocol RestaurantsViewControllerDelegate {
func numberOfRestaurants(in restaurantsViewController: RestaurantsViewController) -> Int
func restaurantsViewController(_ restaurantsViewController: RestaurantsViewController, restaurantAtIndex index: Int) -> Restaurant
func restaurantsViewController(_ restaurantsViewController: RestaurantsViewController, didSelectRestaurant restaurant: Restaurant)
}

不过,我发现的唯一代码表明我必须手动创建每个按钮,就像这样。

@available(OSX 10.12.1, *)
extension NSTouchBarItem.Identifier {
static let restaurant = NSTouchBarItem.Identifier("my.custom.identifier")
}

extension RestaurantsViewController: NSTouchBarDelegate {
override func makeTouchBar() -> NSTouchBar? {
let touchBar = NSTouchBar()
touchBar.delegate = self
touchBar.defaultItemIdentifiers = [.restaurant]
return touchBar
}

func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
switch identifier {

case NSTouchBarItem.Identifier.restaurant:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "This is a restaurant", target: nil, action: nil)
return item
default: return nil
}
}
}

有什么办法可以实现我想做的事情吗?

最佳答案

NSTouchBarItem.Identifier 可以基于任何字符串,包括基于 Restaurant 对象的某些标识符的动态字符串。您可以将 defaultItemIdentifiers 设置为动态生成的标识符数组,然后将为每个标识符调用 touchBar(_:,makeItemForIdentifier:) (然后您可以动态地为每个标识符创建一个按钮或其他方式)。

另一种选择是使用 NSScrubber 。这可能是触摸栏中的单个项目,但它有一个数据源,可用于根据您的餐厅数据源填充它。

关于swift - 动态 NSTouchBar 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52096989/

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