如何子类化NSArrayController在添加时选择文本字段: [英] How can I subclass NSArrayController to select a text field on add:

查看:184
本文介绍了如何子类化NSArrayController在添加时选择文本字段:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,这很简单,但是我仍在学习中。

This is simple I am sure, but I am still very much learning.

我有一个NSTableView,它连接到阵列控制器以显示coredata对象。该表不可编辑。当选择单个记录时,将设置一个子视图以使其可见,该子视图包含选择的值。

I have an NSTableView that is connected to an array controller to display coredata objects. The table is uneditable. When a single record is selected, I have a subview set to be made visible that holds the value of the selection.

我正在尝试做到这一点,因此当我按下连接的+按钮添加时:在阵列控制器上,将创建一个新条目,并且焦点将跳转子视图中的项目描述文本字段,以便用户可以立即开始键入,而不必选择新的rangedObject行,然后在出现子视图时选择文本字段。

I'm trying to make it so that when I press the + button connected to add: on my array controller, a new entry will be made and the focus will jump to the item description text field in the subview so that a user could immediatily begin typing without having to select the new arrangedObject row and then the textfield when the subview appears.

有什么想法吗?

我会发布屏幕截图,但我来这里的时间还不够长。

I would post screenshots but I haven't been a user on here long enough.

推荐答案

Big Nerd Ranch的可可书(第4版)在第9章中提供了此示例。与其使用NSArrayController的-add: +按钮的方法,他们使用自定义方法创建对象,将其插入数组,处理进行中的编辑和撤消管理器分组,最后选择所需的字段进行编辑。以下是摘录:

Big Nerd Ranch's Cocoa book (4th edition) has an example of this in Chapter 9. Instead of using NSArrayController's -add: method for the + button, they use a custom method to create the object, insert it into the array, deal with in-progress edits and undo manager groupings, and finally select the desired field for editing. Here's the excerpt:

- (IBAction)createEmployee:(id)sender
{
NSWindow *w = [tableView window];
// Try to end any editing that is taking place
BOOL editingEnded = [w makeFirstResponder:w];
if (!editingEnded) {
    NSLog(@"Unable to end editing");
    return; }
NSUndoManager *undo = [self undoManager];
// Has an edit occurred already in this event?
if ([undo groupingLevel] > 0) {
    // Close the last group
    [undo endUndoGrouping];
    // Open a new group
    [undo beginUndoGrouping];
}
// Create the object
Person *p = [employeeController newObject];
// Add it to the content array of ’employeeController’
[employeeController addObject:p];
// Re-sort (in case the user has sorted a column)
[employeeController rearrangeObjects];
// Get the sorted array
NSArray *a = [employeeController arrangedObjects];
// Find the object just added
NSUInteger row = [a indexOfObjectIdenticalTo:p];
NSLog(@"starting edit of %@ in row %lu", p, row);
// Begin the edit in the first column
[tableView editColumn:0
                  row:row
            withEvent:nil
               select:YES];
}

完整实现位于 https://github.com/preble/Cocoa4eSolutions

这篇关于如何子类化NSArrayController在添加时选择文本字段:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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