问题
Hoping again for someone to explain me a probably basic question. I´ve setup an imagabutton like this:
class overviewImageButton: NSButton {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
self.image = NSImage(named: "buttonImage.png")
}
}
Now, if I start the application, the image is not shown from start. Pressing the (empty) button, the image shows up. Someone knows how to initialize the button to show the image from start?
Thanks a lot!
回答1:
The problem is that you are putting this code in the function draw for the button so it is not occurring until that time. If you want something to be true from its inception, you should put it in the constructor of the object, so the image code should be in a function with the name override init()
.
It should probably be something like:
override init() {
self.image = NSImage(named: "buttonImage.png")
}
来源:https://stackoverflow.com/questions/44728599/how-to-initialize-an-image-button