Aaron Hillegass _Mac OS X 的 Cocoa 编程_ 第 9 章问题

发布于 2024-08-20 05:39:12 字数 1836 浏览 10 评论 0原文

在 Aaron Hillegass 的 Mac OS X 的 Cocoa 编程 的第 9 章中,名为“开始编辑插入”的部分中,他解释了如何做到这一点。但令我困惑的是,他还做了很多其他事情。这是完整的代码清单:

- (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];
}

In Aaron Hillegass' Cocoa Programming for Mac OS X, Chapter 9, the section called "Begin Editing on Insert", he explains how to do exactly that. The thing that confused me though, was that he did a bunch of other stuff. Here's the full code listing:

- (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];
}

I have two questions regarding this:

1) How do you know you're supposed to do all that stuff? Is there a 'checklist' or something in Apple's doc? Experience?

2) Doesn't this defeat the whole purpose of an array controller if you're having to still rewrite all the methods on your own?

EDIT: I'm mainly wondering how he knew to put these lines in: (since everything else is pretty basic and obvious)

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-08-27 05:39:13

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

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

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

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

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

有人可能会说,像这样修改表格视图行为的能力是数组控制器的全部要点(作为应用程序设计的一个元素)。

1) How do you know you're supposed to do all that stuff? Is there a 'checklist' or something in Apple's doc? Experience?

You're right, that code wouldn't occur to most people doing an initial implementation. (I guess that's why it's in the book. You get to benefit from Aaron's experience).

That code would have come as the result of one or more bug reports. In other words, you wouldn't come up with that code initially, but you would eventually.

Try it for yourself. Remove that code then see if you can spot the problems in the running application. Solving those problems requires a combination of SDK knowledge and debugging skill. Both grow with experience.

2) Doesn't this defeat the whole purpose of an array controller if you're having to still rewrite all the methods on your own?

One could argue that the ability to modify the tableview's behavior like that is the whole point of the array controller (as an element of your application's design).

围归者 2024-08-27 05:39:13

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 控件的经验。嘿=]我不知道......也许有人有更好的答案。我很想学习!

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

1) He's doing the things that implement the features his program requires. Its not so much an Apple thing (like what delegate methods do I have to implement when adhering to such-and-such protocol) but this is his program's flow. There are probably a million ways to solve this problem.

2) Not sure what you mean, but he seems to be using a lot of built in methods - I dont see him really re-inventing the wheel (example: )

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

EDIT

Ok, so for the first message to w, [w makeFirstResponder:w]; we can find from here (http://developer.apple...NSWindow/makeFirstResponder) that an instance of NSWindow supports the makeFirstResponder message. My understanding to do this to NSWindow is so that when the user interacts with it, it will be the first responder, in other words, it will receive any actions for the NSWindow. And by it, I mean 'w'.

1) How do you know you're supposed to
do all that stuff? Is there a
'checklist' or something in Apple's
doc? Experience?

Great question - I think it comes with experience and working with all the different types of classes and UI Controls. heh = ] I don't know ... maybe someone has a better answer. I'd love to learn!

Found a good link: http://www.cocoadev.com/index.pl?FirstResponder

梦归所梦 2024-08-27 05:39:13

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

I think most likely he implemented it without those lines, there were undo problems, and he debugged and fixed the issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文