【问题标题】:New NSWindow from application - mission impossible?来自应用程序的新 NSWindow - 不可能完成的任务?
【发布时间】:2010-12-14 21:32:35
【问题描述】:

好吧,我做错了什么?
1。创建了 cocoa app 和 appDelegate 命名为:window2AppDelegate
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,buttonclick 动作到按钮
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 窗口 IBOutlet 的新 xib 文件,该文件从其控制器连接到窗口(也将控制器设置为 PrefWindowController) 选项“启动时可见”未选中!我想在 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.在我构建并运行应用程序后:closebutton 关闭应用程序但 buttonclick - 不会显示 PrefWindow!?为什么和我做错了什么?不要告诉我在可可objective-c 中显示其他窗口比在“愚蠢的”Java 或C# 中更难?

【问题讨论】:

  • 当您进入-buttonClick: 时,您是否检查过以确保ctrl 不是nil
  • 是的,就像在 init 中一样,但结果相同 - 它是 nil

标签: objective-c cocoa xib nswindowcontroller


【解决方案1】:

我终于解决了这个问题!在 PrefWindow 的 nib 编辑器中,我必须这样做:将文件的所有者类设置为:NSWindowController,然后将窗口 IBOutlet 从文件的所有者连接到我的(首选项)窗口。经过 6 天的不成功尝试后,谷歌开始工作。
无论如何,感谢您的所有回复和时间!

【讨论】:

  • 我打算建议您仔细检查这些设置,但是当您说“创建了名为 PrefWindow 窗口 IBOutlet 的新 xib 文件从其控制器连接到窗口(也将控制器设置为 PrefWindowController)”时,我接受了“也将控制器设置为 PrefWindowController”部分表示您已经将文件的所有者类设置为 PrefWindowController。
【解决方案2】:

我建议您将 PrefWindowController 的创建移至 applicationDidFinishLaunching:

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

【讨论】:

  • 如果委托是 NIB 中的自定义对象,它的 init 应该被调用,但在 applicationDidFinishLaunching 中创建 PrefWindowController 仍然是更好的形式:
【解决方案3】:
- (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 {

}

此时,您的所有 IBOutlets 都应该是有效的(前提是它们不是故意引用一个单独的、尚未加载的 nib 中的对象)。

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

【讨论】:

  • 好吧,我似乎仍然做错了什么我放 if (ctrl == nil) { ctrl = [[PrefWindowController alloc] init]; if ([ctrl window] == nil) NSLog(@"好像窗口是 nil!\n"); } 进入 buttonClick 方法,但它仍然写入输出:“似乎窗口为零!”并且没有首选项窗口:-( awakeFromNib 中具有相同行的相同结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
  • 2015-12-29
  • 1970-01-01
  • 2015-01-26
  • 2022-01-22
  • 1970-01-01
相关资源
最近更新 更多