Setting cursor

I'm trying something that I thought very easy. No chance.


I have a view, with a button. I want to set the cursor to pointing hand when move mouse over.


I tried the following, but no cursor change at all:


class ViewController: NSViewController {

    @IBOutlet weak var modalButton: NSButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        let aCursor = NSCursor.pointingHandCursor()
        modalButton.addCursorRect(modalButton.bounds, cursor: aCursor)
        aCursor.setOnMouseEntered(true)
    }
}


In the documentation, they say to to invoke

invalidateCursorRectsForView:
.

but I get an unresolved identifier error when trying to do so.


I looked at Cursor doc, but very limited info.

Answered by Claude31 in 116083022

It was in fact quite easy :


class ViewController: NSViewController {
    @IBOutlet weak var nameField: NSTextField!   
    @IBOutlet weak var modalButton: NSButton!
   
    private var pointingHand: NSCursor?
    private var openHand : NSCursor?
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
        pointingHand = NSCursor.pointingHandCursor()
        modalButton.addCursorRect(modalButton.bounds, cursor: pointingHand!)
        pointingHand!.setOnMouseEntered(true)
        modalButton.addTrackingRect(modalButton.bounds, owner: pointingHand!, userData: nil, assumeInside: true)
       
        openHand = NSCursor.openHandCursor()
        modalButton.addCursorRect(modalButton.bounds, cursor: openHand!)
        openHand!.setOnMouseExited(true)
        modalButton.addTrackingRect(modalButton.bounds, owner: openHand!, userData: nil, assumeInside: true)
}

You need to call addCursorRect inside an override of resetCursorRects(). If the cursor never changes you probably won't need to call invalidateCursorRectsForView but if that still gives you problems you should post the actual code making the call.

When I try


override func resetCursorRects()


in the ViewController class,


class ViewController: NSViewController


I get an error message :


Method does not override any method from its superclass


So, where should I override this method ?

PS: I use a storyboard for this OS X project.

Accepted Answer

It was in fact quite easy :


class ViewController: NSViewController {
    @IBOutlet weak var nameField: NSTextField!   
    @IBOutlet weak var modalButton: NSButton!
   
    private var pointingHand: NSCursor?
    private var openHand : NSCursor?
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
        pointingHand = NSCursor.pointingHandCursor()
        modalButton.addCursorRect(modalButton.bounds, cursor: pointingHand!)
        pointingHand!.setOnMouseEntered(true)
        modalButton.addTrackingRect(modalButton.bounds, owner: pointingHand!, userData: nil, assumeInside: true)
       
        openHand = NSCursor.openHandCursor()
        modalButton.addCursorRect(modalButton.bounds, cursor: openHand!)
        openHand!.setOnMouseExited(true)
        modalButton.addTrackingRect(modalButton.bounds, owner: openHand!, userData: nil, assumeInside: true)
}
Setting cursor
 
 
Q