繁体   English   中英

如何子类化NSArrayController在添加时选择文本字段:

[英]How can I subclass NSArrayController to select a text field on add:

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

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

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

有任何想法吗?

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

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

- (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

该类仅在完全不需要事情运行的任何控制时才适用。 如果需要控制,则应创建自己的控制器对象,该对象是数据源和表视图的委托,并具有NSFetchedResultsController来访问核心数据。

我个人只使用过NSArrayController作为原型。 一旦开始认真对待应用程序,我总是将其丢弃,并在其位置放置一个自定义的书面控制器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM