Doesn't initialize initWithWindowNibName Window field

I am trying to use the following code. It does what is expected the first time and opens a window and makes it the front window, but when called subsequent it orderFront:

doesn't work because the window is zero. Why initWithWindowNibName:

doesn't it set the field of the object window NSWindowController

that is returned from initWithNibName:

?

//
//  CustomerCard.m
//  POSWonder
//
//  Created by kaydell on 2/26/12.
//  Copyright 2012 Kaydell Leavitt. All rights reserved.
//

#import "CustomerCard.h"

@implementation CustomerCard

// declare customerCard as a static variable
static CustomerCard* customerCard;

+(void) show {

    // if the customer card isn't instantiated, then instantiate it
    if (customerCard == nil) {
        customerCard = [[CustomerCard alloc] initWithWindowNibName:@"CustomerCard"];
        if (!customerCard.window) {
            NSLog(@"Why is window nil here?"); // <<<<<<<<<<< This line gets called <<<<<
        }
    }

    // show the customer card and make it the front window
    [customerCard showWindow:self];
    [customerCard.window orderFront:self]; // <<<<<<<< This line doesn't seem to do anything

}

-(void) dealloc {
    customerCard = nil;
    [super dealloc];
}

@end

      

+3


source to share


2 answers


I know this is an old question, but I wanted to answer my own question anyway.

  • I think using static variables and singleton maps for clients is not a good idea.

    // declare the client card as a static variable
    static client card * customerCard;

Now it seems to me that whenever you use static variables, you defeat the purpose of object oriented programming. Perhaps the user wants more than one customer card to view multiple customers in different windows.



  • My idea of ​​using a class method: "show", always showing the same customer card is not a good idea either. Now I think the user should have the freedom to choose New Customer Card from the File menu, or return to an existing customer card using the Window menu.

This is what I think now.

0


source


In the Builder interface, you need to uncheck the "Release When Closed" checkbox. If this check box is enabled, the window will be released and probably released when it is closed.



If you want to keep the window around, you don't want this behavior, so you need to disable it.

+2


source







All Articles