3

我正在尝试使用以下代码来工作。它第一次执行预期的操作并打开一个窗口并将其设置为前窗口,但是当它随后被调用时,orderFront:由于 window 为 nil 而不起作用。为什么不设置从返回的对象initWithWindowNibName:的窗口字段?NSWindowControllerinitWithNibName:

//
//  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
4

2 回答 2

2

在 Interface Builder 中,您需要取消选中标有“Release When Closed”的框。如果启用此复选框,则窗口将被释放并可能在关闭时被释放。

如果要保留窗口,则不希望出现这种行为,因此需要将其关闭。

于 2012-04-02T00:57:05.770 回答
0

我知道这是一个老问题,但我还是想回答我自己的问题。

  • I think that my using static variables and a singleton for the Customer Card is not a good idea.

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

It seems to me now that whenever you use static variables, you are defeating the purpose of object-oriented programming. Maybe the user wants to have more than one customer card to view more than one customer in different windows.

  • My idea of using the class method: "show", to always show the same Customer Card, is not a good idea either. I think now that the user should have the freedom to choose "New Customer Card" from the "File" menu, or go back to an existing Customer Card using the "Window" menu.

That's what I think now.

于 2013-07-29T21:59:48.817 回答