- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 AppKit 上,菜单项和工具栏项分别有 validateMenuItem(_:)
和 validateToolbarItem(_:)
。然而,对于新的触摸栏项目,没有这种方便的方法来在正确的时刻验证适当的项目。
我现在每次更改相关值并调用 didSet
中的验证方法时都会验证触摸栏项目(请参阅以下示例代码)。但我觉得这不是一个好方法,因为相关值必须知道有一个依赖它的触摸栏项目。
var foo: Foo? {
didSet {
if #available(macOS 10.12.1, *), NSClassFromString("NSTouchBar") != nil {
self.validateTouchBarItem(identifier: .foo)
}
}
}
@available(macOS 10.12.1, *)
func validateTouchBarItem(identifier: NSTouchBarItemIdentifier) {
guard
let item = self.touchBar?.item(forIdentifier: identifier),
let button = item.view as? NSButton
else { return }
switch identifier {
case NSTouchBarItemIdentifier.foo:
button.isEnabled = (self.foo != nil)
default: break
}
}
我使用的另一种方法是 Cocoa 绑定(bind)和 KVO,但是,它并不总是有效。
所以,我很好奇是否有任何推荐的或事实上的标准方法来验证触摸栏项目,尤其是包含 NSButton 和 NSSegmentedControl 的项目。我不仅想改变元素的可用性,有时还想根据情况改变它们的图像或颜色。你们如何验证触摸栏项目?
最佳答案
我自己改进了我的touch bar验证系统,做了如下协议(protocol)和扩展。
@available(macOS 10.12.1, *)
protocol TouchBarItemValidations: class {
func validateTouchBarItem(_ item: NSTouchBarItem) -> Bool
}
@available(macOS 10.12.1, *)
extension NSTouchBarProvider {
func validateTouchBarItems() {
guard NSClassFromString("NSTouchBar") != nil else { return } // run-time check
guard let touchBar = self.touchBar else { return }
// validate currently visible touch bar items
for identifier in touchBar.itemIdentifiers {
guard let item = touchBar.item(forIdentifier: identifier) as? NSCustomTouchBarItem else { continue }
item.validate()
}
}
}
@available(macOS 10.12.1, *)
extension NSCustomTouchBarItem: NSValidatedUserInterfaceItem {
func validate() {
// validate content control
if let control = self.control,
let action = control.action,
let validator = NSApp.target(forAction: action, to: control.target, from: self)
{
if let validator = validator as? TouchBarItemValidations {
control.isEnabled = validator.validateTouchBarItem(self)
} else if let validator = validator as? NSUserInterfaceValidations {
control.isEnabled = (validator as AnyObject).validateUserInterfaceItem(self)
}
}
}
// MARK: Validated User Interface Item Protocol
public var action: Selector? {
return self.control?.action
}
public var tag: Int {
return self.control?.tag ?? 0
}
// MARK: Private Methods
private var control: NSControl? {
return self.view as? NSControl
}
}
@available(macOS 10.12.1, *)
extension AppDelegate {
func applicationDidUpdate(_ notification: Notification) {
// validate touch bar items
if #available(macOS 10.12.1, *) {
if let window = NSApp.mainWindow {
for responder in sequence(first: window.firstResponder, next: { $0.nextResponder }) {
responder.validateTouchBarItems()
}
}
}
}
}
如果您已经有了 AppDelegate 的 applicationDidUpdate(:_)
,您应该修改它,但除此之外,没有什么复杂的要添加。您现在可以使用 validateTouchBarItem(_:)
或普通的 validateUserInterfaceItem(_:)
来验证您自己的触摸条项目!
我想这至少能满足我的要求。
关于swift - 验证 NSTouchBar 项目的最佳实用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40799131/
我正在使用 SDL 创建一个与 OpenGL 一起使用的窗口,它返回的唯一信息是 NSWindow 对象。 我可以使用它来将 NSTouchBar 与该窗口关联起来吗? 我已经通过直接修改 SDL 代
我指的是这里的代码:NSTouchBar Catalog 如何在没有 Touch Bar 的 Macbook/Mac 上测试此功能?我希望我们仍然可以以某种方式模拟它?我尝试在 Mac 上运行此目录,
我正在将 TouchBar 支持集成到我的应用程序中。我使用了 Rey Wenderlich 的方法并按如下方式实现了所有内容: 如果 self.touchBarArray 填充了 makeTouch
我正在为我的应用程序创建一个 NSTouchBar。当该栏显示时,我在左侧看到此 ESC 图标,在右侧看到这些其他图标: 有没有办法摆脱它们并让整个酒吧区域可用? 最佳答案 右侧的图标是“控制条”。
我想知道是否可以动态创建 NSTouchBar 项目。在我的应用程序中,我有一个数据源协议(protocol)来填充 NSTableViewController 的内容。我想使用此协议(protoco
我正在开发一个在触摸栏上显示当前加密货币价格的宠物项目。我是 Web 开发人员,但不是 Swift 开发人员,所以进展缓慢。 https://github.com/sharkattackhq/cryp
在 AppKit 上,菜单项和工具栏项分别有 validateMenuItem(_:) 和 validateToolbarItem(_:)。然而,对于新的触摸栏项目,没有这种方便的方法来在正确的时刻验
我正在尝试通过插件向我无法修改的应用程序添加对公开 NSTouchBar 按钮的支持。该插件是一个共享库,在创建主窗口后在运行时加载。我创建了一个 AppDelegate 如下: @interface
有没有办法将 NSPopoverTouchBarItems 数组添加到 NSScrollView 中? 目前,我的 View 层次结构类似于以下列表。 NSTouchBar NSCustomTouch
我目前正在创建一个小型 open source app预览新 Touch Bar 的内部结构。我有一个带有拖放 View 的窗口,可以通过其 URL 接收图像。以及一个 IB Touch Bar,其中
我是 swift 的新手,一直在通过教程学习如何自己完成它。我能够在单击时更改按钮的标题。我想动态更改标签的标题。它不一定需要像按钮一样点击。 这是 NStouchbar 的标签。 下面的代码是我如何
我目前正在为我的 macOS 应用程序实现 NSTouchBar api。 此刻,我拥有的唯一触摸栏将主视图 Controller 作为其委托(delegate),我可以很好地向其添加项目。问题是,我
问题 我想使用 C++ 创建一个使用新的 macbook pro 触摸条的应用程序。但是我找不到任何真正好的资源。苹果没有任何关于使用 C++ 编写触摸条的文档。 我做了什么 我找到了 this关于
AddressBook 应用程序如何显示“完成”而不是像屏幕截图那样显示 Esc? 文档指出“系统提供的”按钮取决于上下文。我如何定义该上下文? 最佳答案 找到了。您需要使用 NSPopoverTou
我正在尝试将触摸栏支持与 SDL 应用程序集成。 我已经研究了 SDL 中的 Objective C 代码,并直接侵入 SDLView 代码并获得了一个可以工作的触摸栏,但现在我想通过库存 SDL2
我正在使用 Storyboard为我的应用程序构建一个 NSTouchBar。 我想用其他东西替换 ESC 按钮。 像往常一样,没有医生告诉您如何做到这一点。 我在网上搜索过,发现了一些模糊的信息,例
我设计了一个包含对触摸栏的支持的应用程序。触摸栏是在 Interface Builder 中创建的。所有代码都使用 Swift 的 #available 检查来确保在 10.12 之前的 macOS
我知道在 Swift 中可以使用#available,而 Objective-C 不支持这一点。 还有没有办法制作一个可以在 10.11 上运行但也支持 NSTouchbar(从 10.12 开始)的
很高兴尝试新的 NSTouchBar API。去了developer site获取示例代码,每次在 Xcode 8.1(版本 8B62)中都会崩溃,说我没有安装 10.12.1 SDK,即使我可以添加
我是一名优秀的程序员,十分优秀!