How to open a new window in Cocoa application at startup

I created a cocoa application (non-document based) and had the default class MyAppDelegate and MainMenu nib file. I also created a new nib that contains a Splash window and a window controller class (NSWindowController) called SplashWindowController.

I would like that when starting the application, instead of opening the MainMenu nib window, I would like to open the Splash window.

I think I need to instantiate my SplashWindowController in my AppDelegate class, and then instantiate the window and set it to the foreground. However, I tried several things, such as including a link to the SplashWindowController.h file in my AppDelegate class, as well as adding an object to my MainMenu nib and setting its class to SplashWindowController. But they were also unlucky.

If anyone could help me with this, he would really appreciate what was in it (which seems like a simple task) for most of the day.

Thanks in advance.

+3
source share
1 answer

You can simply merge 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 Splash Window
  • Connect both IBOutlets to the corresponding windows
  • Connect the button to the corresponding IBAction
  • Enable "Visible at startup" for the splash window (using the Inspector)
  • Disable "Visible at startup" for the main window (using the Inspector)

enter image description here

Result

. 10 . , . .

+9