将焦点移动到NSTableView中的新添加的记录 [英] Move focus to newly added record in an NSTableView

查看:190
本文介绍了将焦点移动到NSTableView中的新添加的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Core Data编写一个应用程序来控制几个NSTableView。我有一个添加按钮,使一个新的记录在NSTableView。当单击此按钮时,如何使焦点移动到新记录,以便我可以立即键入其名称?在iTunes中,点击添加播放列表按钮后,键盘焦点移动到新行,这样就可以输入播放列表的名称。

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.

推荐答案

首先,首先,如果你还没有一个,你需要为你的应用程序创建一个控制器类。为 NSArrayController 添加一个用于存储对象的插座,以及用于显示您的对象的 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 NSArrayController that your objects are stored in, and an outlet for the NSTableView that displays your objects, in the interface of your controller class.

IBOutlet NSArrayController *arrayController;
IBOutlet NSTableView *tableView;

将这些插座连接到 NSArrayController NSTableView 。然后,您需要创建一个 IBAction 方法,当您按下添加按钮时调用该方法;调用 addButtonPressed:或类似的东西,在你的控制器类接口中声明:

Connect these outlets to the NSArrayController and the NSTableView in IB. Then you need to create an IBAction method that is called when your "Add" button is pressed; call it addButtonPressed: or something similar, declaring it in your controller class interface:

- (IBAction)addButtonPressed:(id)sender;

,并将其作为IB中添加按钮的目标。

and also making it the target of your "Add" button in IB.

现在您需要在控制器类的实现中实现此操作;此代码假定您添加到数组控制器的对象是 NSString s;如果不是,则将 new 变量的类型替换为您要添加的任何对象类型。

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 new variable to whatever object type you are adding.

//Code is an adaptation of an excerpt from "Cocoa Programming for
//Mac OS X" by Aaron Hillegass
- (IBAction)addButtonPressed:(id)sender
{
//Try to end any editing that is taking place in the table view
NSWindow *w = [tableView window];
BOOL endEdit = [w makeFirstResponder:w];
if(!endEdit)
  return;

//Create a new object to add to your NSTableView; replace NSString with
//whatever type the objects in your array controller are
NSString *new = [arrayController newObject];

//Add the object to your array controller
[arrayController addObject:new];
[new release];

//Rearrange the objects if there is a sort on any of the columns
[arrayController rearrangeObjects];

//Retrieve an array of the objects in your array controller and calculate
//which row your new object is in
NSArray *array = [arrayController arrangedObjects];
NSUInteger row = [array indexOfObjectIdenticalTo:new];

//Begin editing of the cell containing the new object
[tableView editColumn:0 row:row withEvent:nil select:YES];
}

当您点击添加按钮时,新行第一列中的单元格将开始编辑。

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.

这篇关于将焦点移动到NSTableView中的新添加的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆