views:

57

answers:

1

Hello,

I have an NSArrayController whose content is the "servers" property of my AppDelegate.

I have an NSTextField which is bound to the "selection.name" property of the NSArrayController and I have buttons attached to the "add:" and "remove:" actions on the NSArrayController.

What I'd like to see happen, is when I click the "Add" button, the NSTextField becomes the first responder so that users can immediately edit the name of the server.

My current strategy is to observe the "arrangedObjects" property of the NSArrayController but all I get back from the NSKeyValueChangeKindKey is the NSKeyValueChangeSetting enum. I was hoping to get the NSKeyValueChangeInsertion value so I could ask the text field to become the First Responder.

My question is, am I going about this the right way or is there a more common way to accomplish this? I'm doing fine hooking up bindings, I'm just struggling with how to insert my own behavior in the cracks.

+1  A: 

Create a subclass of NSArrayController with an outlet to the text field. Override the add: method; in your implementation, immediately send the message up to super, then tell the window to make the text field the first responder.

Peter Hosey
Thanks for your help. For some reason, I had to send the makeFirstResponder: message with performSelector:withObject:afterDelay: (after calling super) to make it take.
Michael Bishop
You probably need the delay since -[NSArrayController add:] internally delays its work.
Mike Abdullah