从视图控制器访问NSButton子类属性时的EXC_BAD_ACCESS

时间:2016-02-08 02:14:30

标签: swift cocoa

我试图从视图控制器访问NSButton子类属性,但在HoverButton的setImages方法中收到以下错误:

  

EXC_BAD_ACCESS(code = 1,adress = 0x0)

我似乎失去了参考,但我不确定如何维护参考。 (对Swift和OS X dev来说还是非常新鲜的)

在我的视图控制器中:

@IBOutlet weak var newAccountButton: HoverButton!
@IBOutlet weak var joinAccountButton: HoverButton!
...
let off = NSImage(named: "button-off")!
let over = NSImage(named: "button-over")!
newAccountButton.setImages(imageOriginal: off, imageHover: over)
joinAccountButton.setImages(NSColor.clearColor(), imageOriginal: off, imageHover: over)

...

class HoverButton: NSButton {

    var trackingArea:NSTrackingArea!
    var hoverBackgroundImage: NSImage!
    var originalBackgroundImage: NSImage!


    override init(frame frameRect: NSRect) {

        super.init(frame: frameRect)

        // set tracking area
        trackingArea = NSTrackingArea(rect: bounds, options: [NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways], owner: self, userInfo: nil)
        self.addTrackingArea(trackingArea)

    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)

        // set tracking area
        trackingArea = NSTrackingArea(rect: bounds, options: [NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways], owner: self, userInfo: nil)
        self.addTrackingArea(trackingArea)
    }

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

    override func mouseEntered(theEvent: NSEvent) {
        let cell = self.cell as! NSButtonCell
        cell.image = hoverBackgroundImage

    }

    override func mouseExited(theEvent: NSEvent) {
        let cell = self.cell as! NSButtonCell
        cell.image = originalBackgroundImage
    }

    func setImages(imageOriginal: NSImage, imageHover: NSImage) {
        self.originalBackgroundImage = imageOriginal
        self.hoverBackgroundImage = imageHover
    }

}

基于https://github.com/Swift-Kit/JZHoverNSButton(使用它会产生同样的错误)

1 个答案:

答案 0 :(得分:2)

问题是您的笔尖或故事板中的HoverButton实例没有出口到joinAccountButton< trapz2D.m > function out = trapz2D(x,y,f) x = x(:); y = y(:); nx = length(x); ny = length(y); dx = diff(x); dy = diff(y); dS = dy*dx.'; df = (f(1:ny-1,1:nx - 1) + f(2:ny,1:nx-1) + f(1:ny-1,2:nx) + f(2:ny,2:nx))/4; out = sum(sum(dS.*df)); end 属性。