Data Synchronization

Now it’s time to add functionality to the ToDoDocument class to facilitate synchronizing data display with an Info window. The Info window must display information about the ToDoItem currently selected in the ToDoDocument and update its view if a new document is opened (or brought to the front). Also, the ToDoDocument must update its display when an item is modified in the Info window. In this section you’ll make this communication work.

The controlTextDidEndEditing: method in ToDoDocument.m is where ToDoItems are added, removed, or modified, so you must let the Info window know when there’s a change in the current ToDoItem. Modify the method to post a ToDoItemChangedNotification, passing in selectedItem as the object related to the notification.

  1. Open ToDoDocument.m.

  2. Import InfoWindowController.h. While you’re here, import SelectionNotifyMatrix.h as well, to pick up the RowSelectedNotification declaration.

  3. Add the following code to the controlTextDidEndEditing: method:

        /* ... */
        [self updateChangeCount:NSChangeDone];
    
        [[NSNotificationCenter defaultCenter] postNotificationName:
                ToDoItemChangedNotification object:selectedItem
                userInfo:nil];
  4. Open InfoWindowController.m and modify the windowDidLoad method to register the InfoWindowController as an observer of the ToDoItemChangedNotification:

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(selectedItemChanged:)
            name:ToDoItemChangedNotification object:nil];
  5. Add the definition of the ...

Get Learning Cocoa now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.