我现在每次更改相关值并在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绑定和KVO,但是,它并不总是很好用.
所以,我很好奇是否有任何推荐或事实标准的方式来验证触摸条项目,特别是包含NSbutton和NSSegmentedControl.我不仅要改变物品的可用性,还要根据情况改变它们的图像或颜色.你们如何验证触控条项目?
解决方法 我自己改进了触摸条验证系统并制作了以下协议和扩展.@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: { .nextResponder }) { responder.valIDatetouchbarItems() } } } }}
你应该修改AppDelegate的applicationDIDUpdate(:_),如果你已经有一个,但除此之外,没有任何复杂的添加.您现在可以使用valIDatetouchbarItem(_ :)或普通的valIDateUserInterfaceItem(_ :)来验证您自己的触控条项目!
我认为这至少可以满足我的要求.
总结以上是内存溢出为你收集整理的swift – 验证NSTouchBar项目的最佳实用方法全部内容,希望文章能够帮你解决swift – 验证NSTouchBar项目的最佳实用方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)