On Oct 26, 2012, at 1:52 AM, "Gerriet M. Denkmann" <[email protected]> wrote:
> > On 26 Oct 2012, at 01:11, Seth Willits <[email protected]> wrote: > >> On Oct 25, 2012, at 4:36 AM, Gerriet M. Denkmann wrote: >> >>> This works, but I have a strong feeling that there is a very obvious better >>> solution which I somehow cannot see. >> >> >> There actually isn't an obvious solution. > > Strange. > I thought that having some InfoPanel which shows data of the current > NSDocument would be a fairly common scenario. > But anyway. > > I decided to follow your suggestions. > > My app delegate now has: > @property (strong) GmdDocument *currentDocument; > > In applicationDidFinishLaunching: it registers for > NSWindowDidBecomeMainNotification and NSWindowWillCloseNotification (not for > NSWindowDidResignMainNotification because I want my InfoPanel to keep it's > data when some other app becomes active). > > The notification method is: > > - (void)someWindowChanged: (NSNotification *)noti > { > NSWindow *window = [ noti object ]; > > if ( ![ window isMemberOfClass: [ NSWindow class ] ] ) return; // > ignore non-Windows This check is wrong because it is too strict; it will fail for NSWindow subclasses including NSPanel. The proper check here is -isKindOfClass:. But it's also completely unnecessary because nothing besides NSWindows will ever post NSWindowDidBecomeMain/WillCloseNotification. Delete it. > > NSWindowController *windowController = [ window windowController ]; > if ( windowController == nil ) return; // ignore strange windows NSWindow has a -document accessor. Use that instead of going though the window controller. > > GmdDocument *document = [ windowController document ]; > if ( ![ document isKindOfClass: [ GmdDocument class ] ] ) return; // > ignore strange documents This will bail out early without setting currentDocument if the new window has no document. That's probably not want you want. > > if ( [ [ noti name ] isEqualToString: NSWindowWillCloseNotification ] ) > document = nil; > > self.currentDocument = document; > } Here's how I would write this method: - (void)someWindowChanged:(NSNotification *)note; { if([note.name isEqualToString:NSWindowDidCloseNotification]) { self.currentDocument = nil; } else /* NSWindowDidBecomeMainNotification */ { NSWindow *window = note.object; NSDocument *doc = window.document; if ([window isKindOfClass:[GmdDocument class]]) self.currentDocument = doc; else self.currentDocument = nil; } } Actually, I wouldn't write this method at all. I'd use the new block-based NSNotification API and register different blocks for NSWindowDidCloseNotification and NSWindowDidBecomeMainNotification. --Kyle Sluder _______________________________________________ Cocoa-dev mailing list ([email protected]) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
