Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
Hello,

I am trying to create multiple instances of an NSWindow without the use of NSDocument and NSDocumentController. For my purpose, it is overkill. I am using NSWindowController, instead. Here is the code that creates an instance of my NSWindow once, a button is clicked.

Code:
    newFileWindowController = [[NewFileWindowController alloc] initWithWindowNibName:@"NewFileWindowController"];
    [newFileWindowController showWindow:nil];

When the button is clicked again, no new instances appear.

The window's nib is connect to the controller via File's Owner. (I just used the standard Xcode 4 new Window Controller template.) Is it possible, though, to make multiple instances of that same window appear when the button is clicked without the use of NSDocument or NSDocumentController. The NSWindowController's documentation is very limited in what it says.

Thanks!
 
In your snippet it looks like you are just replacing the controller - to have multiple windows you will need to create multiple controllers.
 
I never know how many times a user would like to que up a new window though. How could I create a new controller every time the user wants to make a new window? I thought that an NSWindowController would be able to create multiple instances of a specific NSWindow. Are you sure that a new controller has to be created?

Thanks!
 
Each time the button is pressed, just add the new controller instance to an array or whatever. Each window controller manages a window.
 
Thanks for your response.

I'm stuck on trying to do this:

just add the new controller instance to an array

I can't think of a way to dynamically create instances because the instance would require a new name every time it is created. I've looked through the web and the Apple Docs; I can't seem to find a method of creating new instances every time a method is called.

Could you please provide some insight on how I should do that?

Thanks for all your help!
 
Each time you create a new controller with your alloc/init

Code:
[[NewFileWindowController alloc] initWithWindowNibName:@"NewFileWindowController"];

the instance of the new object is returned. You can do whatever with it, such as put it into an variable (array or whatever), so that you can later use it to refer to that object (e.g. to give the window a name or access whatever else you have placed in that window).
 
Ok,

Here is my new code:

Code:
    NSMutableArray *newFileWindowControllerArray  = [[NSMutableArray alloc]init];
    [newFileWindowControllerArray addObject:[[NewFileWindowController alloc]initWithWindowNibName:@"NewFileWindowController"]];
    
    NSUInteger elementsInArray = [newFileWindowControllerArray count];
    
    NSLog(@"%lu",(unsigned long)elementsInArray);
    
    
    [[newFileWindowControllerArray objectAtIndex:i] showWindow:nil];

    
    i++; //Initialized as i =0;

For some reason, the window flashes open and then closes very quickly when this method is called. Furthermore, when the button is clicked again I get an exception stating that i > bounds. It turns out that it's not allowing for more than one instance in the array as the number of elements stays at 1 no matter how many times I click.

I'm not sure what's wrong with my code, to my knowledge it should work.

Any Ideas?

Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.