Whats the appropriate way to react to the delete key in an NSOutlineView - cocoa

I want to remove an item from my NSOutlineView when the user presses the delete key. Is the only way to do this to over-ride the keyUp method of NSResponder? I was hoping for higher level solution.

Its possible to do this without subclassing. Set the NSOutlineView's nextResponder (using -setNextResponder:) to your controller. In the controller class you can override -keyDown: (or -keyUp: and then handle the event in your controller class.

As of 10.10 you don't need to subclass the view to override keyDown(_:) or keyUp(_:). This is because NSViewController inherits from NSResponder and is now automatically added to the responder chain. This means you can override the methods in your view controller subclass instead which is often more convenient.

I think that overriding -keyDown: or -keyUp: in a subclass is the only way to do this.

Related

registerForDraggedTypes with NSStatusBarButton?

I'm trying to implement file drag and drop with an NSStatusBarItem.
All I really have is statusBarItem.button!.window?.registerForDraggedTypes([NSFilenamesPboardType]), but I don't know how or where to actually override methods like draggingEntered and performDragOperation.
I know it can be done with a custom view, but I'd like to stay away from deprecated methods if possible.
NSButton inherits from NSView. Therefore you can overlay a custom view which implements drag and drop functionality and add it as a subview to the NSButton. This approach avoids deprecated methods.

controlTextDidBeginEditing is not being called when NSTextField is selected

controlTextDidBeginEditing delegate method is not being called when NSTextfield is selected by clicking or by pressing tab.The delegate is set and all other delegate methods are fired.Any suggestions??
To handle key events like Tab key press can be handled by writing setFieldEditor: method in the delegate method.For this requirement this method is suffice.
Another method is to override the NSTextfield class and writing keyDown: method.

How to make a view controller first responder for an NSView in Cocoa

I'm trying to implement a view controller for a custom NSOpenGLView based view (this is Cocoa, not Cocoa Touch).
The view is contained within a NIB loaded window but it does not have its own NIB. In fact the window contains multiple instances of the view.
I want to route mouse events to the controller instead of to the view. I would like for this to happen as soon as the user clicks within the corresponding view.
So how can this be done ?
I've tried having the view's becomeFirstResponder method call makeFirstResponder with the controller as argument. However that doesn't seem to work, the view still receives the mouse events instead of the controller if NSView::becomeFirstResponder returns YES. If it returns NO then neither of my classes receive the mouse events.
Of course I could implement the mouse event handling methods in the view and explicitly forward them to the controller but it seems like there should be a better way to handle this.
For general "first responder" status, I recommend Charles Parnot's MTViewController, an NSViewController subclass that uses KVO to make certain the controller is in the responder chain with no extra effort on your part.
However, in your case, you want mouse events too. There's really no way around this - your view will need to translate mouse events into controller interactions.

What to subclass for clickedRow (NSTableView)

Which method should I subclass to change the drawing when a row is right-mouse clicked? (I want to change it in selecting the row).
It sounds to me like you want the highlightSelectionInClipRect: method.
If you're looking to make a source list (like in iTunes, Mail, and the Finder), you should use the setSelectionHighlightStyle: method (or set that property in IB) instead.
Implement the delegate method tableView:willDisplayCell:forTableColumn:row:

How to get notifications of NSView isHidden changes?

I am building a Cocoa desktop application. I want to know when a NSView's isHidden status has changed. So far using target/action doesn't help, and I can't find anything in NSNotification for this task. I would like to avoid overriding the setHidden method, because then I'll have to override all the NSView derived class that I am using.
UPDATE: I ended up using KVO. The path for "isHidden" is "hidden", probably because the setter is "setHidden".
You could use Key-Value Observing to observe the isHidden property of the NSView(s). When you receive a change notification from one of these views, you can check if it or one of its superviews is hidden with -isHiddenOrHasHiddenAncestor.
A word of warning: getting Key-Value Observing right is slightly tricky. I would highly recommend reading this post by Michael Ash, or using the -[NSObject gtm_addObserver:forKeyPath:selector:userInfo:options] method from the NSObject+KeyValueObserving category from the Google Toolbox for Mac.
More generally, one can override viewWillMoveToWindow: or the other related methods in NSView to tell when a view will actually be showing (i.e. it's window is in the window display list AND the view is not hidden). Thus the dependency on KVO for the 'hidden' key used above is removed, which only works if setIsHidden has been called on that view. In the override, 'window' (or [self window]) will indicate whether the view is being put into a visible view hierarchy (window is non-nil) or being taken out of it (window is nil).
I use it for example to start/stop a timer to update a control from online data periodically - when I only want to update while the control is visible.
Could you override the setter method for the hidden property so that it will trigger some custom notification within your application?

Resources