0

我正在制作一个使用 NSTouchBar 的应用程序。touchbar 是由 NSWindowController.makeTouchBar() 方法制作的。在这个触摸栏中,我可以放置 NSCustomTouchBarItems。我制作了两个 NSCustomTouchBarItems。

第一个将视图设置为默认的 ui 按钮,其中:

let item001 = NSCustomTouchBarItem(identifier: someIdentifier)
item001.view = NSButton(title: "myButton", target: nil, action: nil)

第二个设置了一个 viewController,其中:

let item002 = NSCustomTouchBarItem(identifier: someIdentifier)
item002.viewController = TestViewController()

TestViewController 仅在其 loadView() 方法中加载一个简单视图。

class TestViewController: NSViewController {
    
    override func loadView() {
        
        self.view = TestView001(frame: NSRect(x: 0, y: 0, width: 100, height: 30))
    }
}

TestView001 只创建一个背景颜色,以便您可以看到它。TestView001 有以下代码:

class TestView001: NSView {

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        print("TestView001.init()")
        
        // Create a background color.
        self.wantsLayer = true
        self.layer?.backgroundColor = NSColor.green.cgColor
    }
    
    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

所有这一切都完美无缺。

但是当我触摸触摸栏中的第二个项目时,然后关闭我的应用程序窗口。windowController 和其他一切都很好地从内存中释放。但是我仍然可以看到 TestView001 在内存中并且没有被释放。

当使用像 item001 这样的标准 ui 按钮时,您就不会遇到这个问题。

如果您查看此图像,似乎某些 NSTouch 仍然具有对视图的引用:但是,我并不完全理解此图像。

在此处输入图像描述

解决这个问题的最佳方法是什么。提前致谢。

4

0 回答 0