为什么 NSToolbarItem 会自动禁用? [英] Why does NSToolbarItem get disabled automatically?

查看:25
本文介绍了为什么 NSToolbarItem 会自动禁用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Mac 幻灯片应用工具栏中的按钮有一些问题.

I have a bit of an issue with buttons in the toolbar of my slideshow application for Mac.

我希望在没有活动幻灯片放映时禁用这些按钮,并在有活动幻灯片放映时启用这些按钮.为了实现这一点,我在开始时将按钮的 isEnabled 属性设置为 false(我已经尝试了 Interface Builder 和我的窗口的 windowDidLoad) 然后在我的幻灯片变量的 didSet 中,我执行以下操作:

I want those buttons to be disabled when there is no active slideshow and enabled when there is an active slideshow. In order to achieve this, I set the buttons' isEnabled property to false at the start (I have tried both Interface Builder and my window's windowDidLoad) and then in the didSet of my slideshow variable I do the following:

var slideshow: Slideshow? {
    didSet {
        self.playPauseButton.isEnabled = slideshow != nil
    }
}

所发生的情况是按钮(在本例中只有一个)启用了大约半秒钟,然后再次禁用.

What happens is that the buttons (only one in this example) are enabled for about half a second and then disabled again.

我在 didSet 上设置了一个断点,以确认幻灯片在某些时候未设置为 nil.我也没有在我的代码中的任何其他地方修改 isEnabled.为了测试,我设置了 isEnabled = true 而不是 false 只是为了看看会发生什么,结果按钮保持启用状态.

I've set a breakpoint on the didSet to confirm that slideshow isn't set to nil at some point. I'm also not modifying isEnabled anywhere else in my code. For testing I've set isEnabled = true instead of false just to see what happens and it turns out the buttons stay enabled then.

AppKit 的某些部分是否会出于某种我不知道的原因自动禁用按钮?我希望有人能有想法.

Might there be some part of AppKit that automatically disables the buttons for some reason I don't know about? I hope someone might have an idea.

推荐答案

好的,我设法解决了这个问题.

Okay, I managed to fix the problem.

事实证明,NSToolbarItems 的验证确实存在问题,就像链接到的问题 matt 一样.然而,简单地覆盖我的 NSWindowController 子类中的函数 validateToolbarItem 并没有完成这项工作,因为没有为其中包含视图的 NSToolbarItems 调用该函数(请参阅 Apple 的文档).

It did turn out to be an issue with the validation of NSToolbarItems as in the question matt linked to. However, simply overriding the function validateToolbarItem in my NSWindowController subclass did not do the job as the function is not called for NSToolbarItems with views in them (see Apple's documentation).

为了解决这个问题,我必须子类化 NSToolbarItem 并覆盖它的 validate 函数以在其目标上调用 validateToolbarItem,如问题 此处.

To get around that, I had to subclass NSToolbarItem and override its validate function to call validateToolbarItem on its target as described in the answer to the question here.

这篇关于为什么 NSToolbarItem 会自动禁用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆