2011-05-19 102 views
3

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

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

我认为我必须在我的AppDelegate类中创建一个SplashWindowController的实例,然后实例化窗口并将其设置在前面。然而,我已经尝试了几个东西,比如在我的AppDelegate类中包含对SplashWindowController.h文件的引用,并且还将一个对象添加到我的MainMenu笔尖并将其类设置为SplashWindowController。但是两者都没有运气。

如果有人可以帮助我这个,它将非常感谢,因为在这一天(似乎是一个简单的任务)一天的最佳部分。

在此先感谢。

回答

9

您可以简单地将两个窗口组合成一个.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(标题:飞溅)
  • 既IBOutlets添加NSButton到飞溅窗口
  • 连接到相应的窗口
  • 按钮连接到相应IBAction为
  • 启用防溅窗口(使用检查员)
  • “在发射可见”
  • 禁用 '可见于启动' 的主窗口(使用检查)

enter image description here

结果

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

相关问题