I have a NSCollectionView populated using binding via NSArrayController.

All i am trying is to add an observer to either NSArrayController or NSCollectionView so that when ever the selection changes i got a call a function.

For example this is what i am doing for NSTextField change :

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.textDidChange(_:)), name: NSTextViewDidChangeSelectionNotification, object: nil)

I am looking for the correct way to do it.

1

There are 1 best solutions below

0
Anand On

Able to achieve with this simple code :

Added this to applicationDidFinishLaunching :

self.fileListArrayController.addObserver(self, forKeyPath: "selectionIndexes", options: .New, context: nil)  

And this function gets called every time user/program selects an item in my NSArrayController

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {  
print(keyPath)  
}