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
}
}
무엇을하려고합니까, 왜 하위 클래스가 필요합니까? 마우스 다운 이벤트로'mouseUp'을 호출하면 올바르게 작동하지 않습니다. – Willeke
저는 버튼이 어떻게 보이는지에 대한 제어권을 얻고 롤오버와 다운 상태를 추가하려고합니다. 저는 이미지를 사용하고 싶지 않습니다. 그렇다면 어떻게 작동하는지 이해하고 싶습니다. –