cocoa - 不 initWithWindowNibName 设置窗口字段

标签 cocoa nswindow nswindowcontroller

我正在尝试使用以下代码来工作。它第一次执行预期的操作,打开一个窗口并将其设为前窗口,但是当随后调用它时, 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/

相关文章:

objective-c - 使用 NSLayoutManager 停止文本换行

iphone - 打开并操作 .xcdatamodeld-File/SQLite 的记录?

macos - 如何在其 contentView 上拖动 NSWindow 和 NSImageView ?

macos - 进入全屏模式时显示 NSToolbar

objective-c - 如何给 NSWindow 一个摇动效果,就像在登录失败窗口中一样说不

cocoa - 界面生成器中的 NSWindowController 对象链接

swift - 在 Swift 中没有 nib 文件的 NSWindowController

objective-c - Cocoa Key事件问题

objective-c - 在 Objective-C 中构建重复的 UI

iphone - CGPath 中的 CGGradient