从外部类访问触摸栏

时间:2018-10-29 10:20:54

标签: swift xcode macos nstouchbar macbookpro-touch-bar

我想将计时器触摸栏小部件移到右侧的控制条。我已经找到了可以通过一些私有API来执行此操作的代码,但是我无法找到如何将windowController连接到它。

这是我的windowController

import Cocoa

class WindowController: NSWindowController {

    @IBOutlet var touchButton: NSButton!
    @IBOutlet var touchbar: NSTouchBar!
    var delegate: NSTouchBarDelegate?

    var timer = Timer();

    override func windowDidLoad() {
        super.windowDidLoad()

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    }

    @IBAction func startTimer(_ sender: Any) {
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateTimer), userInfo: nil, repeats: true);
    }

    @objc func updateTimer() {
        touchButton.title = TimeHandler().convertTimeDiffToString(timeDiff: TimeHandler().calcTimeDiff(currentTime: TimeHandler().getCurrentTimeInSeconds()));
    }
}

这是可以将我的按钮移至控制条的代码,但是我无法从该类访问触摸栏

import Cocoa

class TouchBarController: NSObject, NSTouchBarDelegate {

    static let shared = TouchBarController()
    let touchBar = NSTouchBar()

    func setupControlStripPresence() {
        DFRSystemModalShowsCloseBoxWhenFrontMost(false)
        let item = NSCustomTouchBarItem(identifier: touchButton)
        item.view = NSButton(image: #imageLiteral(resourceName: "Strip"), target: self, action: #selector(touchbar))
        NSTouchBarItem.addSystemTrayItem(item)
        DFRElementSetControlStripPresenceForIdentifier(touchButton, true)
    }
}

0 个答案:

没有答案