我正在尝试使用以下代码来工作。它第一次执行预期的操作,打开一个窗口并将其设为前窗口,但是当随后调用它时, orderFront: 不起作用,因为 window 为 nil。为什么 initWithWindowNibName:
不设置从 initWithNibName:
返回的 NSWindowController
对象的窗口字段?
//
// 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
最佳答案
在 Interface Builder 中,您需要取消选中标有“关闭时释放”的框。如果启用此复选框,窗口将被释放,并可能在关闭时被释放。
如果您想保留窗口,您不希望出现这种行为,因此您需要将其关闭。
关于cocoa - 不 initWithWindowNibName 设置窗口字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9969394/