5

在 Aaron Hillegass 的Cocoa Programming for Mac OS X的第 9 章,名为“Begin Editing on Insert”的部分中,他解释了如何做到这一点。不过,让我感到困惑的是,他做了很多其他的事情。这是完整的代码清单:

- (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]) {
    // 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];
[p release];
// 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
int row = [a indexOfObjectIdenticalTo:p];
NSLog(@"starting edit of %@ in row %d", p, row);

// Begin the edit in the first column
[tableView editColumn:0
                  row:row
            withEvent:nil
               select:YES];
}

我对此有两个问题:

1)你怎么知道你应该做所有这些事情?Apple的文档中是否有“清单”或其他内容?经验?

2)如果您仍然必须自己重写所有方法,这是否会破坏数组控制器的全部目的?

编辑:我主要想知道是怎么知道把这些线放进去的:(因为其他一切都非常基本和明显)

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]) {
    // Close the last group
    [undo endUndoGrouping];
    // Open a new group
    [undo beginUndoGrouping];
}
4

3 回答 3

4

1)你怎么知道你应该做所有这些事情?Apple的文档中是否有“清单”或其他内容?经验?

你是对的,大多数人在进行初始实现时不会想到该代码。(我想这就是它出现在书中的原因。你可以从 Aaron 的经验中受益)。

该代码可能是一个或多个错误报告的结果。换句话说,你最初不会想出那个代码,但你最终会想出的。

自己试试吧。删除该代码,然后查看您是否可以在正在运行的应用程序中发现问题。解决这些问题需要结合 SDK 知识和调试技能。两者都随着经验而成长。

2)如果您仍然必须自己重写所有方法,这是否会破坏数组控制器的全部目的?

有人可能会争辩说,像这样修改 tableview 的行为的能力是数组控制器的重点(作为应用程序设计的一个元素)。

于 2010-02-03T04:20:14.177 回答
2

1)他正在做实现他的程序所需的功能的事情。它与其说是苹果的事情(就像我在遵守某某协议时必须实现的委托方法一样),但这是他的程序的流程。可能有一百万种方法可以解决这个问题。

2)不知道你的意思,但他似乎使用了很多内置方法 - 我没有看到他真的重新发明轮子(例如:)

Person *p = [employeeController newObject];

// Add it to the content array of 'employeeController'
[employeeController addObject:p]; // <-- built in method
[p release]; // <-- built in method
// Re-sort (in case the user has sorted a column)
[employeeController rearrangeObjects]; // <-- built in method

// Get the sorted array
NSArray *a = [employeeController arrangedObjects]; // <-- built in method

// Find the object just added
int row = [a indexOfObjectIdenticalTo:p]; // <-- built in method

编辑

好的,所以对于给 w 的第一条消息,[w makeFirstResponder:w]; 我们可以从这里 ( http://developer.apple...NSWindow/makeFirstResponder ) 找到 NSWindow 的一个实例支持 makeFirstResponder 消息。我对 NSWindow 的理解是,当用户与之交互时,它将成为第一响应者,换句话说,它将接收 NSWindow 的任何操作。我的意思是'w'。

1)你怎么知道你应该做所有这些事情?Apple的文档中是否有“清单”或其他内容?经验?

很好的问题 - 我认为它带有经验和使用所有不同类型的类和 UI 控件。heh = ] 我不知道...也许有人有更好的答案。我很想学习!

找到了一个很好的链接:http ://www.cocoadev.com/index.pl?FirstResponder

于 2010-02-03T03:17:25.483 回答
1

我认为他很可能在没有这些行的情况下实现了它,存在撤消问题,他调试并修复了这些问题。

于 2010-02-03T04:21:40.880 回答