如何初始化图像按钮? [英] How to initialize an image button?

查看:104
本文介绍了如何初始化图像按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次希望有人向我解释一个可能是基本的问题。我已经设置了一个像这样的imagabutton:

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?

非常感谢!

推荐答案

问题是您将此代码放在按钮的函数绘制中,因此直到那时才发生。如果您希望某件事从一开始就是正确的,则应将其放入对象的构造函数中,因此图像代码应位于名称为 override init()。

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().

它应该类似于:

override init() {
    self.image = NSImage(named: "buttonImage.png")
}

这篇关于如何初始化图像按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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