3

次のコードを機能させようとしています。1回目は期待どおりにウィンドウを開いてフロントウィンドウにするのですが、2回目以降は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 で、「閉じたら解放する」というラベルの付いたボックスのチェックを外す必要があります。このチェックボックスが有効になっている場合、ウィンドウは解放され、おそらく閉じたときに割り当てが解除されます。

ウィンドウを保持したい場合は、この動作が望ましくないため、オフにする必要があります。

于 2012-04-02T00:57:05.770 に答える