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

查看:100
本文介绍了为什么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的验证相比,这确实是一个问题.但是,仅覆盖我的NSWindowController子类中的函数validateToolbarItem并不能完成该工作,因为没有为其中包含视图的NSToolbarItems调用该函数(

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的方式调用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天全站免登陆