Aaron Hillegass _ Cocoa Programming for Mac OS X_ Chapter 9 Question

In Aaron Hillegass Cocoa for Mac OS X, Chapter 9, the section “Start editing when pasting,” he explains how to do this. What confused me was that he did a bunch of other things. Here is a complete list of codes:

- (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 that you should do all this? Is there a checklist or something in the Apple document? An experience?

2) Doesn't that destroy the whole purpose of the array controller if you still need to rewrite all the methods yourself?

EDIT: I basically wonder how he knew to put these lines: (since everything else is pretty solid 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];
}
+5
3

1) , ? " " - Apple? ?

, , . ( , . ).

. , , .

. , . SDK . .

2) , ?

, - ( ).

+4

1) , , . Apple (, , , - ), . , .

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

EDIT

, w, [w makeFirstResponder: w]; (http://developer.apple...NSWindow/makeFirstResponder), NSWindow makeFirstResponder. NSWindow , , , , NSWindow. "w".

1) , ? " " - Apple ? ?

- , . heh =] ... , - . !

: http://www.cocoadev.com/index.pl?FirstResponder

+2

I think that most likely he implemented it without these lines, there were problems with cancellation, and he debugged and fixed the problems.

+1
source

All Articles