Why is my applicator window closing?

I have a tip that I download in the usual way

[NSBundle loadNibNamed:@"AuthorizationWindow" owner:self];

and I see the window on the screen for a short while, and using NSLog (), I can confirm that -awakeFromNib was called, but I can't figure out why the window doesn't stay on the screen. I worked on this for a bit, but now I'm not sure what I changed, what messed it up. Thoughts on where to start looking?

+1


source to share


4 answers


I would suggest that your window is freed (or, if under GC, collected) right from under you. There are about a million possible reasons for this (none of which we can diagnose from a single line of code), but the fact that you are using loadNibNamed: owner: is a warning flag. This is because the elements instantiated follow the same memory management rules as the rest of the Cocoa; if you want them to stick, you have to store them (or in the GC, keep a reference to them). NSWindowController (and NSViewController too) has some special nib handling code so that it keeps all top-level objects in its tip when it loads, so they will stick as long as it does *. However, if you are not using this, you must do it all manually.

Real solution: Don't use + loadNibNamed: owner :. Instead, subclass NSWindowController and configure it -init like so:



@implementation AuthorizationWindowController
- (id)init
{
    self = [super initWithWindowNibName:@"AuthorizationWindow"];
    if (self == nil) return nil;
    // any other initialization code
    return self;
}

      

* It also has special code to handle the held bindings loops that usually cause it to leak, which is quite difficult to write yourself. Another reason for using NSWindowController.

+3


source


I would take a closer look at the object you are using to load the nib ( self

in the code you provided). Since he is the owner of the window, the window will be released when this object is released.



0


source


It turns out that I turned on GC and it did it. Gives the window controller method. Thank.

0


source


In the header file windowViewController put this:

@property (strong) NSWindowController *wc;

      

in implementation: synthesize wc top

-(id)init{
    wc = [super initWithWindowNibName:@"NewWindowController"];
    if(wc == nil){
        return nil;
    }

    return wc;
}

      

If you want to make the window visible:

- (IBAction)mnuNewImageClicked:(id)sender {
    NewWindowController *ivc = [[NewWindowController alloc] init];
    [ivc showWindow:self];
}

      

This worked for me.

0


source







All Articles