Re: Multiple instances of same window
Re: Multiple instances of same window
- Subject: Re: Multiple instances of same window
- From: Neil Earnshaw <email@hidden>
- Date: Tue, 8 Jul 2003 12:48:10 +0100
Hi John,
I'm working on a drawing program that does the sort of thing you want
to do. My drawings are hierarchical; a DrawModel has a number of
DrawRects, which can have a number of DrawModels and so on. The app
allows you to open a browser on any DrawModel or DrawRect. The browser
presents an NSBrowser that allows you to browse the drawing hierarchy.
This window is analogous to your ShowInfo window because you can have
many of them open, each with its own subject. Here are the main design
components.
Browsable Protocol
My browser has to cope with many types of objects. The Browsable
protocol defines a standard interface that all browser subjects must
implement. If you are inspecting many different types of object, you
might want to define an Inspecting protocol.
BrowserWindow.nib
I made BrowserWindowController the File's Owner. The controller is not
instantiated in the nib because the controller is the one that's going
to load the nib. I set the window to 'Release when closed'.
BrowserWindowController
The key method for creating a new browser looks like this:
-(id)initWithSubject:(id <Browsable>)subject
{
// NSLog(@"BrowserWindowController initWithSubject:");
if ( self = [super initWithWindowNibName:@"BrowserWindow"] ) {
[self setSubject:subject];
[[self window] makeKeyAndOrderFront:self];
}
return self;
}
The caller passes the subject in so that each browser knows what its
looking at. -setSubject just sets the instance variable
id<Browsable> _selectedBrowsable; // Not retained
and registers for notifications so it can update when the subject
changes. If the var was retained then things get awkward when you
delete the subject from the model. My browser registers for
<Browsable>WasDeleted notifications issued when model components are
deleted and kills itself if its subject is the one that was deleted.
-(void)browsableWasDeleted:(NSNotification*)note
{
if ( _subject == [note object] ) {
[self close];
}
else {
// update browser's views
}
}
DrawWindowController
The drawing window provides 'Browse' menu options for the drawing
background and the rects that its displaying. Here is the menu action
that is called when the menu item is selected.
-(IBAction)browseRect:(id)sender
{
//NSLog(@"DrawWindowController browseRect");
BrowserWindowController* ctl = [[BrowserWindowController alloc]
initWithSubject:_selectedDrawRect];
[ctl autorelease];
[[self document] addWindowController:ctl];
}
The DWC knows which rect was selected. It creates a new
BrowserWindowController and calls -initWithSubject: to tell the browser
what it's browsing. The browser controller is then attached to the
document so that the document can close it etc.
A word of warning: I'm a relative newbie, so there might be a better
way to do this.
Hope this helps,
Neil
Neil Earnshaw
Consultant Software Engineer
Object Software Engineers Ltd
email@hidden
Tel : 01747 854 852
Mbl : 07870 209 102
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.