Currently I’m learning Obj-C for Mac developing, with Cocoa. I made a simple file browser with an inspector, to see a file’s icon an some info. So far, so good. Now I made it document based, so I could have more than one open windows.
To tell the inspector which file it should inspect, I use the NSWindowDidBecomeMainNotification. Works fine for switching between windows, but it gives an EXC_BAD_ACCESS when I close all windows and then open a new one.
This is the method that handles the notification:
- (void)windowChanged: (NSNotification *)notification
{
NSWindow *window = [notification object];
BrowserWindow *doc = [[window windowController] document];
if (currentDocument != doc) {
[currentDocument.arrayController removeObserver: self
forKeyPath: @"selectionIndex"];
[icon setImage:nil];
[size setStringValue:@"-"];
[owner setStringValue:@"-"];
[filename setStringValue:@"(none selected)"];
[doc.arrayController addObserver: self
forKeyPath: @"selectionIndex"
options: NSKeyValueObservingOptionNew
context: NULL];
currentDocument = doc;
}
}
The error occurs where it calls removeObserver:forkeyPath: on the currentDocument.arrayController. It kinda makes sense, it tries to remove the observer for something that doesn’t exist anymore, ’cause the window is closed. But how to fix it? I just can’t think of anything else..
Could someone point me in the right directions?
I appreciate the help! 🙂
—
It’s getting weirder.. Just checked the example that was downloadable from the website of the book I’ve got, and they’re using the same approach, but it works all fine. Can’t find any differences, it’s driving me crazy.
—
Solved! More details later.
This is part of the problem right there. When the last window closes, no window will become main. So, you also need to handle the case where a window resigns main, as happens when it closes (and when another window becomes main).
Your inspector probably should both retain the document and switch documents after a delay, using a timer (whose fire date you postpone every time another did become/resign main notification comes in) or delayed perform (which you cancel and re-perform every time). When the timer/perform fires, find out what document, if any, is the active document, and update the inspector accordingly.
Also note that you can have no active document (no document window is the main window) even when there are documents open. The About panel and your Preferences panel are two good ways to achieve (and test) this.