Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Variable number of windows, NOT document based app (cocoa)

Variable number of windows, NOT document based app (cocoa)
Thread Tools
Amorya
Mac Elite
Join Date: Mar 2001
Location: England
Status: Offline
Reply With Quote
Jul 13, 2005, 05:28 PM
 
I need variable numbers of windows, and am not sure how to go about it.

I have a networking controller class. When it receives a certain message from the network, it must create a new window (along with its associated controller and model class instances), and somehow assign it a string as a handle, provided by the network. (So the networking class might be told to create a new window with handle "#foo").

Then later, the network may tell me to send a certain message to the controller of a certain window. For example, "Tell window #foo that Bob says hello". It's kind of like an IRC client in that respect (although I'll be doing more with different kind of messages).

I chose not to use a document based app as I will not be saving files, undoing actions, etc... I think the extra functionality of a document app is not needed.

So I'm trying to work out how to store my multiple windows. I'm thinking an NSMutableDictionary, with the handle as the key, and a pointer to the window as the value. But how do I create a new window? All the windows will be functionally identical, of course - only the data within them will differ. Anyone got any pointers? (no pun intended!)

Amorya
Cocoa newbie
What the nerd community most often fail to realize is that all features aren't equal. A well implemented and well integrated feature in a convenient interface is worth way more than the same feature implemented crappy, or accessed through a annoying interface.
     
Catfish_Man
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Jul 13, 2005, 05:46 PM
 
Have a look at NSWindowController
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 13, 2005, 05:58 PM
 
I would still use NSDocument. Even if you're not using all the functionality of NSDocument, you'll still be recreating some of it — using NSDocument would save you the trouble.

As for creating a bunch of windows from a template, I'd make a window in a nib and then use NSWindowController's initWithWindowNibName to load it from that.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Amorya  (op)
Mac Elite
Join Date: Mar 2001
Location: England
Status: Offline
Reply With Quote
Jul 13, 2005, 06:03 PM
 
Would there be any point in NSDocument? I was under the impression that it liked to bind the documents to a file on disk - which I wouldn't be doing. I'll look into it though...

What about matching up my handles to the windows? NSMutableDictionary sound ok for that?

Thanks for all your help

Amorya
What the nerd community most often fail to realize is that all features aren't equal. A well implemented and well integrated feature in a convenient interface is worth way more than the same feature implemented crappy, or accessed through a annoying interface.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 13, 2005, 08:51 PM
 
NSMutableDictionary would work okay, though it makes more sense to me conceptually if you make the handle an instance variable of the NSWindowController in charge of the window, since it sounds like it's supposed to be a property of the window. But you'd know better than I would.

As for NSDocument, it doesn't really have to represent a file on disk. Many apps that don't necessarily bind to files on the disk, such as Safari and iPhoto if I recall correctly, use NSDocument. Of course, Apple also twists NSTextView to display information in Address Book (which gives me a headache just picturing it), so maybe they're not the most convenient example to follow.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Amorya  (op)
Mac Elite
Join Date: Mar 2001
Location: England
Status: Offline
Reply With Quote
Jul 13, 2005, 08:57 PM
 
I could make the handle an instance variable, but that would simply make it easy to find the handle when you know the window. It's the reverse I'll be doing more often - I have the handle I want to talk to, and need to find its window - and I don't want to have to iterate through all of 'em.

Still not seeing much of an advantage to NSDocument - after reading more into it, it seems I'd have an added complication that a document and its window are not necessarily one-to-one related. But it's 2am, so I may be missing things

Amorya
What the nerd community most often fail to realize is that all features aren't equal. A well implemented and well integrated feature in a convenient interface is worth way more than the same feature implemented crappy, or accessed through a annoying interface.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 13, 2005, 09:19 PM
 
Nah, like I said, you know what you're doing better than I do (I wasn't even sure exactly what role the handle played). I was just throwing out general ideas based on Cocoa apps I've done. If it sounds harder to you, by all means please ignore me.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
parallax
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Jul 14, 2005, 12:50 AM
 
So I sort of have a similar situation with a terminal program I'm writing (multiple terminal windows). What I did was this,

id terminal = [[TermController alloc] init];
[NSBundle loadNibNamed:@"TermWindow" owner:terminal];

Where basically "TermWindow" is a nib containing a window, some stuff in the window, and a TermController file's owner.

NSWindowController is another good place to look.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 14, 2005, 02:40 AM
 
Out of curiosity, parallax, why did you forgo NSWindowController? I can think of very few situations in which I wouldn't use NSWindowController for a normal window.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
seppak
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Wisconsin
Status: Offline
Reply With Quote
Jul 14, 2005, 06:33 PM
 
Did my sockets class work for you?

I would definitely just subclass NSWindowController and make your class the file owner in the nib file you are using.
     
Amorya  (op)
Mac Elite
Join Date: Mar 2001
Location: England
Status: Offline
Reply With Quote
Jul 14, 2005, 06:47 PM
 
seppak: it certainly did! I've only got the basics of networking so far, but it can certainly talk to a server and receive (and NSLog ) the response.

On the windowing issue, I have got it seemingly working using my NSMutableDictionary method. The hardest part was remembering how to initialise the NSMutableDictionary

I now have:

Code:
@implementation WBLWorldController - (id)init { windowHashes = [[NSMutableDictionary alloc] init]; return self; } - (void)newWindowWithHash:(NSString *)hash { [windowHashes setObject:[[NSWindowController alloc] initWithWindowNibName:@"Tower"] forKey:hash]; [[windowHashes valueForKey:hash] window]; } @end
It's working so far... now to do the referring to windows via their hashes...


Amorya
What the nerd community most often fail to realize is that all features aren't equal. A well implemented and well integrated feature in a convenient interface is worth way more than the same feature implemented crappy, or accessed through a annoying interface.
     
Amorya  (op)
Mac Elite
Join Date: Mar 2001
Location: England
Status: Offline
Reply With Quote
Jul 14, 2005, 07:01 PM
 
Erk - new problem: how do you refer to an object you instantiated in interface builder, from another object which you instantiated in interface builder?

My networking controller needs to send a message to my world controller (to open the new windows). I suddenly realised I have no idea how to do that!

Fixed!
I made it so that only WBLWorldController was instantiated by IB, and instantiated the networking controller myself through code, allowing me to get a handle on it.

Amorya
( Last edited by Amorya; Jul 14, 2005 at 08:12 PM. )
What the nerd community most often fail to realize is that all features aren't equal. A well implemented and well integrated feature in a convenient interface is worth way more than the same feature implemented crappy, or accessed through a annoying interface.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 15, 2005, 01:42 AM
 
Originally Posted by Amorya
Erk - new problem: how do you refer to an object you instantiated in interface builder, from another object which you instantiated in interface builder?
You can drag connections between objects in Interface Builder.

And about your code: It has a leak. Not a major leak, but a leak nonetheless. You add [[NSWindowController alloc] initWithWindowNibName:@"Tower"] to the dictionary, but your object never releases its ownership of that window controller. It should be something like this:

Code:
- (void)newWindowWithHash:(NSString *)hash { NSWindowController *windowController = [[NSWindowController alloc] initWithWindowNibName:@"Tower"]; [windowHashes setObject:[windowController forKey:hash]; [windowController release]; }
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 10:26 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,