I am writing an application using Core Data to control a few NSTableViews. I have an add button that makes a new a record in the NSTableView. How do I make the focus move to the new record when this button is clicked so that I can immediately type its name? This is the same idea in iTunes where immediately after clicking the add playlist button the keyboard focus is moved to the new line so you can type the playlist's name.
Objective-c – Move focus to newly added record in an NSTableView
Okay well first of all, if you haven't already got one, you need to create a controller class for your application. Add an outlet for the
NSArrayControllerthat your objects are stored in, and an outlet for the
NSTableViewthat displays your objects, in the interface of your controller class.
Connect these outlets to the
NSTableViewin IB. Then you need to create an
IBActionmethod that is called when your "Add" button is pressed; call it
addButtonPressed:or something similar, declaring it in your controller class interface:
and also making it the target of your "Add" button in IB.
Now you need to implement this action in your controller class's implementation; this code assumes that the objects you have added to your array controller are
NSStrings; if they are not, then replace the type of the
newvariable to whatever object type you are adding.
This will then be called when you click the "Add" button, and the cell in the first column of the new row will start to be edited.