Search code examples
objective-ccocoainterface-buildernspanel

NSPanel not showing.


My approach to this may be all wrong so I appreciate your patience.

I have a button in my main XIB file linked to this method in my document.m file:

- (IBAction)showTagModal:(id)sender {
    if (!_FileTagWindowController){
        _FileTagWindowController = [[FileTagWindowController alloc]init];
    }
    [_FileTagWindowController showWindow:self];
}

_FileTagWindowController is declared as a property in document.h and using breakpoints when the method is called, as far as I can tell is initializing properly, however _windowNibName and _window remains nil.

FileTagWindowController.h looks like this.

#import <Cocoa/Cocoa.h>

@interface FileTagWindowController : NSWindowController{

}

@property (strong) IBOutlet NSArrayController *tagsArray;



- (IBAction)saveContext:(id)sender;


@end

FileTagWindowController.m looks like this:

#import "FileTagWindowController.h"

@interface FileTagWindowController ()

@end

@implementation FileTagWindowController

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    NSLog(@"Window Did Load!");
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

- (IBAction)saveContext:(id)sender {
}
@end

in my FileTagWindowController.xib I have File Owner set to FileTagWindowController as the custom class. I have the File Owner's "window" outlet linked to the window (NSPanel). That's all that should be required correct? The NSLOG statement in WindowDidLoad never gets called. I tried using [super initWithWindowNibName] in FileTagWindowController.m but that crashes not only the app, but Xcode as well with an endless initialization loop. Am I missing something obvious here?

Thanks all so much.


Solution

  • Try something like the following.

    // document.h
    #import "FileTagWindowController.h"
    
    @property (strong) filetagWindowController *FileTagWindowController;
    
    // document.m
    @synthesize filetagWindowController;
    
    - (IBAction)showTagModal:(id)sender {
    if (self.filetagWindowController == nil) {
        self.filetagWindowController = [[FileTagWindowController alloc] initWithWindowNibName:@"FileTagWindowController"];
    }
    
        [filetagWindowController showWindow:self];
        [[filetagWindowController window] setReleasedWhenClosed:NO];
        [NSApp runModalForWindow:filetagWindowController.window];
        filetagWindowController = nil;
    }
    

    You may also want to call NSWindowWillCloseNotification to observe its state and see if filetagWindowController is closed.