Mike Bedar
Mike Bedar

Reputation: 910

NSButton Subclass not sending event

I'm subclassing NSButton and having a lot of trouble with what I thought would be pretty straight-forward. I am close to where I need to be, visually, but the button no longer seems to send an event when pressed. This button is placed in IB and wired to an action in the containing view's controller class.

Here is the code for the custom NSButton and NSButtonCell classes. You will see in the MouseUp function a few of the ways I have been trying to get this working. The action and target seem to both be null, even though that info should be coming in from IB, I assumed...

Shocked that this question has not been asked and answered a 100 times but if it has, I can't find it.

Also, as a side-question, I notice that the ButtonCell concept seems to be deprecated. Advice on how to do this without using a button cell would also be appreciated!

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
    }
}

Upvotes: 1

Views: 876

Answers (1)

Mike Bedar
Mike Bedar

Reputation: 910

Figured this out. I was not calling super.awakeFromNib(), and I also needed to preserve the original NSButtonCell's action and target before nuking it. I'm going to ditch the cell though - seems totally unneeded.

Upvotes: 1

Related Questions