objective-c - Aaron Hillegass Cocoa Programming for Mac OS X 第 9 章问题

标签 objective-c cocoa xcode cocoa-bindings appkit

在 Aaron Hillegas 的 Mac OS X 的 Cocoa 编程 的第 9 章中,“插入时开始编辑”一节中,他解释了如何做到这一点。不过,让我感到困惑的是,他还做了很多其他事情。这是完整的代码 list :

- (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) 你怎么知道你应该做所有这些事情?苹果文档中是否有“ list ”或其他内容?经验?

2) 如果您仍然必须自己重写所有方法,这不是破坏了数组 Controller 的全部目的吗?

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

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

最佳答案

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

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

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

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

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?

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

关于objective-c - Aaron Hillegass Cocoa Programming for Mac OS X 第 9 章问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2189307/

相关文章:

ios - 检测 SFSafariViewController 中的 URL 更改

ios - 在多天重复 HMTimerTrigger(例如 : Every Monday, 星期三......就像在 iOS 10 Home 应用程序中一样)

ios - 在我的 iPhone 上下载 iOS 应用程序

ios - 双Segue IOS

iphone - 如何在 Cocoa Touch 上延迟的同时保留响应式 GUI?

xcode - 如何在 Mac OS X 上获取内存泄漏的行号堆栈跟踪?

ios - 如何使用 Objective C 将 UIObjects 从一个类重用到多个类?

objective-c - AuthorizationExecuteWithPrivileges 已弃用

iphone - 如何确保我的应用永远不会成为 "Open In..."的默认应用

iphone - 如何正确格式化这些日期?