以编程方式处理NSButton

时间:2014-07-21 21:38:10

标签: macos cocoa swift

我试图让一个子类NSButton处理一个没有运气的点击事件。

到目前为止,我一直在自定义视图中的drawRect函数,以获取自定义外观并以编程方式绘制这些视图。为此,我基本上孤立了我对Interface构建器的使用(只是为了避免其纯粹的复杂性)。

我是否有办法以编程方式分配点击(或此方案中的mouseDown)处理程序,这似乎不起作用

class MainButton: NSButton {

    override func mouseDown(theEvent: NSEvent!) {
        let alert = NSAlert()
        alert.messageText = "Title"
        alert.runModal()
    }

    override func drawRect(rect: NSRect) {
        super.drawRect(rect)

        let circle : NSBezierPath = NSBezierPath(
            roundedRect: rect, xRadius: rect.size.width / 2,
            yRadius: rect.size.height / 2
        )

        circle.lineWidth = 2

        NSColor(calibratedWhite: 0.2, alpha: 1).setStroke()
        circle.stroke()
    }
}

这是a full gist of the app for context if needed

0 个答案:

没有答案
相关问题