As a basis, we'll use Apple's demo app IconCollection. Download it and open it in XCode.
- Open MyViewController.h and add a delegate outlet to the IconViewBox. This will be necessary so we can pass the double click event on to NSCollectionViewItem and from there to the controller. It should look like this:
- Open MyViewController.m and alter the hitTest method to allow clicks and add a method to capture the double click to IconViewBox's implementation:
- Build and run. When you double click on items, you should see a log message in the console.
- Open IconViewPrototype.xib in IB and connect the View's delegate outlet with "File's Owner":
- Save IconViewPrototype.xib.
- Add a new file called IconCollectionItem (both the .h and the .m).
- Open IconViewPrototype.xib again and change the File Owner's class to "IconCollectionItem". Save.
- Open Collection.xib and also change the Collection View Item's class to "IconCollectionItem". Save.
- Build and run. When you double click now, you should see two log messages in the console.
- Finally, we want to be notified of the double click in our controller. So open up MyViewController.h and add a doubleClick method to the @interface:
- Open MyViewController.m and add the method implementation:
- Build and run: When you double click, the event gets passed on all the way to the controller and you'll see a log message on the console telling you which icon you have double clicked on. That's pretty nifty.
Johannes Fahrenkrug said...
Beautiful! Thank you so much!
Much easier if you just override the mouseDown method of your custom item view and use the sendAction method of NSApplication with nil as target.
NSApplication will find your doubleClick method in your controller automatically since the controller is in the responder chain
if([theEvent clickCount] > 1)
[NSApp sendAction:@selector(collectionItemViewDoubleClick:) to:nil from:[self object]];