来自应用程序的新 NSWindow - 不可能完成的任务?

发布于 2024-10-07 07:36:57 字数 2122 浏览 0 评论 0原文

好吧,我做错了什么?
1.创建了可可应用程序和名为:window2AppDelegate的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,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 的新 xib 文件,窗口 IBOutlet 从其控制器连接到窗口(也是设置为 PrefWindowController 的控制器)选项“启动时可见”未选中!我想在单击按钮时看到这个窗口。
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 关闭应用程序,但单击按钮 - 不会向我显示 PrefWindow!?为什么以及我做错了什么?不要告诉我在 cocoa Objective-C 中显示其他窗口比在“愚蠢的”Java 或 C# 中更困难吗?

OK, what am I doing wrong?

1. Created cocoa app and appDelegate named: 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. in xib editor, window connected to window controller - set to appdelegate, buttonclick actions to buttons

4, created

#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. created new xib file named PrefWindow window IBOutlet connected to window from its controller (also controller set to PrefWindowController) Option "Visible At Launch" UNCHECKED! i want to see this window on buttonclick.

6. implemented 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. After I build and run app: closebutton closes the app but buttonclick - won't show me PrefWindow!? Why and what am i doing wrong? Don't dell me that to show other window in cocoa objective-c is more difficult than in "stupid" Java or C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

也只是曾经 2024-10-14 07:36:57

终于我解决了这个问题!在 PrefWindow 的 nib 编辑器中,我必须执行以下操作:将文件的所有者类设置为:NSWindowController,然后将窗口 IBOutlet 从文件的所有者连接到我的(首选)窗口。经过 6 天的尝试失败后,Google 成功了。

无论如何,感谢您的回复和时间!

Finally i've managed the problem! In the nib editor for PrefWindow I had to do: Set File's owner class to: NSWindowController then connect window IBOutlet from File's owner to my (preferneces) window. After 6 days of unsuccessful attempts, google works.

Anyway, thanks for all your responses and time!

晨光如昨 2024-10-14 07:36:57

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

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

I'd suggest you move the creation of the PrefWindowController to applicationDidFinishLaunching:

I am not sure the application delegate's init method is called. Probably only initWithCoder: gets called when unarchiving the object from the NIB.

烟雨凡馨 2024-10-14 07:36:57
- (id) init {
   if ((self = [super init])) {
      ctrl = [[PrefWindowController alloc] init];
   if ([ctrl window] == nil)
      NSLog(@"Seems the window is nil!\n");
   }
   return self;
}

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

- (void)awakeFromNib {

}

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

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

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

init is way too early in the scheme of things to be trying to test IBOutlets. They will still be nil yet. Not until later on in the object creation process will the nib outlets be "hooked up". The standard method where you can know that everything in the nib file has been hooked up is:

- (void)awakeFromNib {

}

At that point, all of your IBOutlets should be valid (provided they're not purposely referencing an object in a separate, yet-unloaded nib).

If PrefWindowController is a class that will only be used after the user chooses Preferences from the app menu, I would have to disagree with the others and say that I would not create the instance of the PrefsWindowController at all during the initial load. (Your main controller should be able to function independently from the prefs window). If you have a method that is meant to load the preferences window, then when that method is called, you should check to see if the PrefsWindowController instance is nil, and if it is, create it, then proceed to show the prefs window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文