NSButton子类不发送事件

时间:2016-10-10 05:23:18

标签: swift macos cocoa

我正在为NSButton创建子类,并且在我认为非常简单的方面遇到了很多麻烦。我在视觉上接近我需要的位置,但按下时按钮似乎不再发送事件。此按钮位于IB中,并连接到包含视图的控​​制器类中的操作。

以下是自定义NSButton和NSButtonCell类的代码。您将在MouseUp函数中看到我尝试使其工作的一些方法。动作和目标似乎都是空的,即使这些信息应该来自IB,我假设......

感到震惊的是,这个问题没有被问过并且已经回答了100次但是如果有的话,我找不到它。

另外,作为一个侧面问题,我注意到ButtonCell概念似乎已被弃用。不使用纽扣电池如何做到这一点的建议也将不胜感激!

import Cocoa

class TestButton: NSButton {
    static let buttonFont = NSFont(name: "Avenir-Black ", size: 14)!
    static let textColor = NSColor(red:0.84, green:1.00, blue:0.60, alpha:1.00)
    static let buttonColorActive = NSColor(red:0.43, green:0.72, blue:0.00, alpha:1.00)
    static let buttonColorDisabled = NSColor(red:0.72, green:0.86, blue:0.50, alpha:1.00)

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override init(frame frameRect: NSRect) {
        super.init(frame:frameRect)
    }

    override class func cellClass() -> AnyClass? {
        return TestButtonCell.self
    }

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
    }

    override func awakeFromNib() {
        // Replace Cell
        let oldCell = self.cell
        let cell = TestButtonCell(textCell: oldCell?.title ?? "TestButton" )
        self.cell = cell

        // Add Tracking
        let tracking = NSTrackingArea(rect: self.bounds, options: [NSTrackingAreaOptions.ActiveAlways ,NSTrackingAreaOptions.MouseEnteredAndExited] , owner: self, userInfo: nil)
        self.addTrackingArea(tracking)

        //Set Background Color
        setBGColor(TestButton.buttonColorActive)
        self.sendActionOn(NSEventMask.LeftMouseUp)
    }

    func setBGColor(color:NSColor){
        if let cell = cell as? TestButtonCell {
            cell.setBGColor(color)
        }
    }

    //Mouse Functions
    override func mouseEntered(event: NSEvent) {
        setBGColor(TestButton.buttonColorDisabled)
    }
    override func mouseExited(event: NSEvent) {
        setBGColor(TestButton.buttonColorActive)
    }

    override func mouseDown(theEvent: NSEvent) {
        self.highlighted = true
        super.mouseDown(theEvent)
        self.mouseUp(theEvent)

    }

    override func mouseUp(theEvent: NSEvent) {
        super.mouseUp(theEvent)
//      Swift.print(self.action, self.target)
//      Swift.print(self.cell?.action, self.cell?.target)
//      sendAction(self.action, to:self)
//      self.performClick(self)
    }
}


class TestButtonCell:NSButtonCell {
    func setBGColor(color:NSColor){
        self.backgroundColor = color
    }

    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(textCell string: String) {
        super.init(textCell: string)

        self.bordered = false

        let style = NSMutableParagraphStyle()
        style.alignment = .Center
        let attributes = [
            NSForegroundColorAttributeName: TestButton.textColor,
            NSFontAttributeName: TestButton.buttonFont,
            NSParagraphStyleAttributeName: style
            ] as [String : AnyObject]

        let attributedTitle = NSAttributedString(string: title, attributes: attributes)
        self.attributedTitle = attributedTitle
    }
}

1 个答案:

答案 0 :(得分:1)

想出来了。我没有调用super.awakeFromNib(),我还需要在核对之前保留原始NSButtonCell的动作和目标。我打算抛弃细胞 - 似乎完全不需要。