问题
I have a bit of an issue with buttons in the toolbar of my slideshow application for Mac.
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.
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.
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.
回答1:
Okay, I managed to fix the problem.
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).
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.
来源:https://stackoverflow.com/questions/49410817/why-does-nstoolbaritem-get-disabled-automatically