2016-10-10 3 views
1

私はNSButtonをサブクラス化していて、かなり簡単に思えるものに多くの問題を抱えています。私は視覚的に必要な場所の近くにいますが、ボタンは押されてもイベントを送信しないように見えます。このボタンはIBに配置され、包含ビューのコントローラクラス内のアクションに配線されます。NSButtonサブクラスがイベントを送信しない

ここにカスタムNSButtonクラスとNSButtonCellクラスのコードを示します。 MouseUp関数には、これを動作させようとしているいくつかの方法があります。アクションとターゲットは、たとえその情報がIBから来ていても、nullであると思われます。

この質問には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 
    } 
} 
+0

あなたは何をしようとしていますか、なぜサブクラスが必要ですか?マウスのdownイベントで 'mouseUp'を呼び出すと、気分が悪くなります。 – Willeke

+0

私はちょうどボタンの外観を制御し、ロールオーバーとダウン状態を追加しようとしています。私は本当にイメージを使用したくないし、それを超えて私は本当にこの作品の仕組みを理解したい。 –

答えて

1

これを実証しました。私はsuper.awakeFromNib()を呼び出すのではなく、元のNSButtonCellのアクションとターゲットを保存してからヌックする必要がありました。しかし、私は細胞を捨てるつもりです - 全く不要なようです。

関連する問題