Skip to main content
 首页 » 操作系统

macos之NSWindow 全尺寸内容 View 和启动弹出窗口导致错误

2024年01月01日31Terrylee

在单击按钮显示弹出窗口时在控制台中看到此错误后:

unlockFocus called too many times. Called on <NSButton: 0x6180001404d0> 

在最终找到问题之前,我复制了我的应用程序并将其剥离到骨头上。 我可以在新项目中成功复制它。

请问是否有人知道为什么会这样,并且知道解决方法,我在下面展示了在新项目中复制此错误的方法......?

创建新的基于文档的项目。 添加一个按钮、一个自定义 View ,以及在弹出 View 中显示自定义 View 所必需的:

class Document: NSDocument { 
 
    @IBOutlet var thebutton: NSButton! 
    @IBOutlet var popver: NSPopover! 
 
    override init() { 
        super.init() 
        // Add your subclass-specific initialization here. 
    } 
 
    @IBAction func thebuttonclicked(sender: AnyObject) { 
        var theview = thebutton 
        var thebounds = thebutton.bounds 
        popver.showRelativeToRect(thebounds, ofView: theview, preferredEdge: NSMinXEdge) 
    } 

这就是导致问题的原因...将 IB 中的窗口设置为“显示全尺寸内容”

运行应用程序,单击按钮打开弹出窗口时会看到控制台错误:

unlockFocus called too many times. Called on <NSButton: 0x6180001404d0> 

虽然弹出窗口始终有效,但不为 IB 中的窗口选择“全尺寸内容 View ”会导致错误永远不会出现。

请您参考如下方法:

所以有了来自同行的有用输入。我找到了一个似乎是操作系统错误的解决方法。

而不是像往常一样调用弹出窗口。喜欢:

@IBAction func thebuttonclicked(sender: AnyObject) { 
    var theview = thebutton 
    var thebounds = thebutton.bounds 
    popver.showRelativeToRect(thebounds, ofView: theview, preferredEdge: NSMinXEdge) 
} 

我必须像这样将 dispatch_async 发送到启动弹出函数:

@IBAction func thebuttonclicked(sender: AnyObject) { 
    dispatch_async(dispatch_get_main_queue()) { 
        self.launchmypopover() 
    } 
} 
 
 
func launchmypopover(){ 
    var theview = thebutton 
    var thebounds = thebutton.bounds 
    popver.showRelativeToRect(thebounds, ofView: theview, preferredEdge: NSMinXEdge) 
} 

我就此问题提交了雷达。在发布修复程序之前,我必须使用此方法。