应用新的NSWindow - 任务不可能吗?

时间:2010-12-14 21:32:36

标签: objective-c cocoa xib nswindowcontroller

好的,我做错了什么?
1。创建了名为:window2AppDelegate的cocoa app和appDelegate
2。 window2AppDelegate.h

#import "PrefWindowController.h"
@interface window2AppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    PrefWindowController * ctrl;
}

@property (assign) IBOutlet NSWindow *window;
- (IBAction) buttonClick:(id)sender;
- (IBAction) buttonCloseClick:(id)sender;
@end


3。在xib编辑器中,窗口连接到窗口控制器 - 设置为appdelegate,按钮单击按钮操作
4,创建

#import <Cocoa/Cocoa.h>
@interface PrefWindowController : NSWindowController {
@private

}
@end

#import "PrefWindowController.h"
@implementation PrefWindowController

- (id)init {
    self = [super initWithWindowNibName: @"PrefWindow"];
    return self;
}

- (void)dealloc {
    // Clean-up code here.
    [super dealloc];
}

- (void)windowDidLoad {
    [super windowDidLoad];
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

@end


5。创建了名为PrefWindow窗口的新xib文件IBOutlet从其控制器连接到窗口(也设置为PrefWindowController的控制器)选项“在启动时可见”UNCHECKED!我想在buttonclick上看到这个窗口。
6。实现了window2AppDelegate

#import "window2AppDelegate.h"
@implementation window2AppDelegate
@synthesize window;

- (id) init {
    if ((self = [super init])) {
        ctrl = [[PrefWindowController alloc] init];
    if ([ctrl window] == nil)
        NSLog(@"Seems the window is nil!\n");
    }
    return self;
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
    return YES;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
}

- (IBAction) buttonClick:(id)sender {
//    [[ctrl window] makeKeyAndOrderFront:self]; this doesn't work too :(
NSLog(@"it is here");
[ctrl showWindow:sender];
}

- (IBAction) buttonCloseClick:(id)sender {
    [window close];
}

@end


7。在我构建并运行应用程序后:关闭按钮关闭应用程序,但按钮点击 - 不会显示我PrefWindow!?为什么以及我做错了什么?不要告诉我,在可可的目标c中显示其他窗口比在“愚蠢的”Java或C#中更难吗?

3 个答案:

答案 0 :(得分:1)

最后我遇到了问题!在PrefWindow的nib编辑器中,我必须这样做:将File的所有者类设置为:NSWindowController 然后将窗口IBOutlet从File的所有者连接到我的(preferneces)窗口。经过6天的尝试失败,谷歌工作。
无论如何,感谢您的所有回复和时间!

答案 1 :(得分:0)

我建议你将PrefWindowController的创建移到applicationDidFinishLaunching:

我不确定是否调用了应用程序委托的init方法。可能只有initWithCoder:在从NIB取消归档对象时被调用。

答案 2 :(得分:0)

- (id) init {
   if ((self = [super init])) {
      ctrl = [[PrefWindowController alloc] init];
   if ([ctrl window] == nil)
      NSLog(@"Seems the window is nil!\n");
   }
   return self;
}

init在试图测试IBOutlets的方案中还为时尚早。它们仍然是零。直到稍后在对象创建过程中,笔尖出口才会“连接”。您可以知道nib文件中的所有内容都已连接的标准方法是:

- (void)awakeFromNib {

}

此时,所有IBOutlet都应该是有效的(前提是它们并非故意引用一个单独但尚未卸载的nib中的对象)。

如果PrefWindowController是一个只在用户从应用程序菜单中选择“”后才会使用的类,我将不得不与其他人不同并说我不会创建PrefsWindowController的实例在初始加载期间。 (您的主控制器应该能够独立于prefs窗口运行)。如果您有一个方法用于加载首选项窗口,然后调用该方法时,您应该检查PrefsWindowController实例是否为nil,如果是,则创建它,然后继续显示prefs窗口。