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
cocoamacosnstableviewobjective-c
Best Solution
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
NSArrayController
that your objects are stored in, and an outlet for theNSTableView
that displays your objects, in the interface of your controller class.Connect these outlets to the
NSArrayController
and theNSTableView
in IB. Then you need to create anIBAction
method that is called when your "Add" button is pressed; call itaddButtonPressed:
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
NSString
s; if they are not, then replace the type of thenew
variable 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.