Question

I have a Cocoa app that uses automatic reference counting and does not use core-data (not document-based) and I want to be able to create multiple instances of a window I have defined in a nib file.

I currently have this in my AppDelegate:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application

    // for slight performance bump, we assume that users usually have 1 session open.
    sessionWindowControllers = [[NSMutableArray alloc] initWithCapacity:1];
}

- (void) awakeFromNib {
    //  on start, we create a new window
    [self newSessionWindow:nil];
}
- (IBAction)newSessionWindow:(id)sender {

    SessionWindowController *session = [[SessionWindowController alloc] initWithWindowNibName:@"SessionWindow"];

    //add it to the array of controllers
    [sessionWindowControllers addObject:session];
    [session showWindow:self];

}

SessionWindowController is a subclass of NSWindowController. but when I run it, I get the runtime error

LayoutManagement[30415] : kCGErrorIllegalArgument: _CGSFindSharedWindow: WID 11845 Jun 8 18:18:05 system-process LayoutManagement[30415] : kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged. Jun 8 18:18:05 system-process LayoutManagement[30415] : kCGErrorIllegalArgument: CGSOrderFrontConditionally: Invalid window

Is using NSMutableArray even a good way to manage multiple windows, or is there a better design pattern? Thanks!

Was it helpful?

Solution

Ans. kindly provided by Ben Flynn:

I put the

sessionWindowControllers = [[NSMutableArray alloc] initWithCapacity:1];

in applicationDidFinishLaunching, but since awakeFromNib is called first, we are trying to add an instance of session to the array before it even exists.

Solution: put array init inside the awakeFromNib before we make our first window.

- (void) awakeFromNib {
    sessionWindowControllers = [[NSMutableArray alloc] initWithCapacity:1];
    [self newSessionWindow:nil];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top