如何在启动时在 Cocoa 应用程序中打开一个新窗口

发布于 2024-11-08 01:48:12 字数 493 浏览 0 评论 0原文

我创建了一个可可应用程序(不是基于文档的)并具有默认的 MyAppDelegate 类和 MainMenu nib 文件。我还创建了一个新的笔尖,其中包含一个名为 Splash 的窗口和一个名为 SplashWindowController 的窗口控制器类 (NSWindowController)。

我想要的是,当应用程序启动而不是打开 MainMenu 笔尖窗口时,我想打开启动窗口。

我认为我必须在 AppDelegate 类中创建 SplashWindowController 的实例,然后实例化该窗口并将其设置为前面。不过,我尝试了一些方法,例如在我的 AppDelegate 类中包含对 SplashWindowController.h 文件的引用,并向我的 MainMenu 笔尖添加一个对象并将其类设置为 SplashWindowController。但两者都没有运气。

如果有人可以帮助我完成这个任务,我将非常感激,因为我一天中大部分时间都在做这个(看起来像是一项简单的任务)。

提前致谢。

I have created a cocoa application (not document based) and have the default MyAppDelegate class and the MainMenu nib file. I have also created a new nib which contains a window called Splash and a window controller class (NSWindowController) called SplashWindowController.

What I would like is that when the application starts instead of the MainMenu nib window opening I would like to open the Splash window.

I think that I have to create an instance of my SplashWindowController in my AppDelegate class and then instantiate the window and set it to front. However I have tried several things like including a reference to the SplashWindowController.h file in my AppDelegate class and also adding an object to my MainMenu nib and setting its class to be SplashWindowController. But have had no luck with either.

If anybody out there could help me with this one it would be much appreciated as have been at this (what seems like a simple task) for the best part of a day.

Thanks in advance.

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

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

发布评论

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

评论(1

尹雨沫 2024-11-15 01:48:12

您可以简单地将两个窗口合并到一个 .xib 文件中。

ExampleAppDelegate.h

#import <Cocoa/Cocoa.h>

@interface ExampleAppDelegate : NSObject <NSApplicationDelegate> {
    IBOutlet id splash;
    IBOutlet id window;
}

- (IBAction)closeSplashButton:(id)sender;
- (void)closeSplash;

@end

ExampleAppDelegate.m

#import "ExampleAppDelegate.h"

@implementation ExampleAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [NSTimer scheduledTimerWithTimeInterval:5.0
                                     target:self
                                   selector:@selector(closeSplash)
                                   userInfo:nil
                                    repeats:NO];    
}

- (IBAction)closeSplashButton:(id)sender {
    [self closeSplash];
}

- (void)closeSplash {
    [splash orderOut:self];
    [window makeKeyAndOrderFront:self];
    [NSApp activateIgnoringOtherApps:YES];
}

@end

MainMenu.xib

  • 添加 NSWindow(标题:Splash)
  • 将 NSButton 添加到 Splash 窗口
  • 将两个 IBOutlet 连接到相应的窗口
  • 将按钮连接到相应的 IBAction
  • 为启动窗口启用“启动时可见”(使用检查器)
  • 为主窗口禁用“启动时可见”(使用检查器)

在此处输入图像描述

结果

启动时仅可见启动窗口。启动窗口将在 10 秒后自动关闭。用户可以通过按下按钮直接关闭启动窗口。关闭启动窗口后将显示主窗口。

You can simply combine both windows into one .xib file.

ExampleAppDelegate.h

#import <Cocoa/Cocoa.h>

@interface ExampleAppDelegate : NSObject <NSApplicationDelegate> {
    IBOutlet id splash;
    IBOutlet id window;
}

- (IBAction)closeSplashButton:(id)sender;
- (void)closeSplash;

@end

ExampleAppDelegate.m

#import "ExampleAppDelegate.h"

@implementation ExampleAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [NSTimer scheduledTimerWithTimeInterval:5.0
                                     target:self
                                   selector:@selector(closeSplash)
                                   userInfo:nil
                                    repeats:NO];    
}

- (IBAction)closeSplashButton:(id)sender {
    [self closeSplash];
}

- (void)closeSplash {
    [splash orderOut:self];
    [window makeKeyAndOrderFront:self];
    [NSApp activateIgnoringOtherApps:YES];
}

@end

MainMenu.xib

  • Add NSWindow (Title: Splash)
  • Add NSButton to the Splash window
  • Connect both IBOutlets to the corresponding windows
  • Connect the button to the corresponding IBAction
  • Enable 'Visible at Launch' for the splash window (using the Inspector)
  • Disable 'Visible at Launch' for the main window (using the Inspector)

enter image description here

Result

At launch only the splash window is visible. The splash window automatically closes after 10 seconds. The user can close the splash window directly by pressing the button. The main windows shows up after closing the splash window.

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