首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在OS X上使用情节提要初始化另一个窗口

在OS X上使用情节提要初始化另一个窗口
EN

Stack Overflow用户
提问于 2014-11-01 21:17:56
回答 3查看 14.2K关注 0票数 27

我已经在Xcode6中创建了一个使用故事板的Cocoa应用程序。作为模板,Xcode为应用程序提供了一个窗口。我想添加第二个窗口来显示程序第一次加载的时间。所以基本上,会出现两个窗口。

我在Main.storyboard上放置了一个窗口控制器,第一个窗口也驻留在那里。但是,我找不到在程序启动时显示第二个窗口的方法。你能帮帮忙吗?

谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-11-29 19:52:33

在故事板中,选择第二个窗口控制器。在标识检查器中,指定此窗口控制器的名称,例如secondWindowController

然后,在您的应用程序委托中,为窗口控制器设置一个属性:

代码语言:javascript
复制
@property NSWindowController *myController;

在您的applicationDidFinishLaunching: method实现中,创建对Storyboard的引用。这样你就可以从故事板访问你的窗口控制器了。在此之后,剩下的唯一要做的就是通过向窗口控制器发送showWindow:方法来显示窗口。

代码语言:javascript
复制
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
@synthesize myController;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSStoryboard *storyBoard = [NSStoryboard storyboardWithName:@"Main" bundle:nil]; // get a reference to the storyboard
myController = [storyBoard instantiateControllerWithIdentifier:@"secondWindowController"]; // instantiate your window controller
[myController showWindow:self]; // show the window
}

@end
票数 41
EN

Stack Overflow用户

发布于 2017-03-11 23:21:23

Swift 3版本:

代码语言:javascript
复制
var myWindowController: NSWindowController!

override init() {
    super.init()

    let mainStoryboard = NSStoryboard.init(name: "Main", bundle: nil)
    myWindowController = mainStoryboard.instantiateController(withIdentifier: "myWindowControllerStoryboardIdentifier") as! NSWindowController
    myWindowController.showWindow(self)
}

确保在函数外部定义了myWindowController,否则窗口将不会显示。

实际上,在(AppDelegate的) init()中做这件事会更好,因为您可能会在前面需要它。

票数 6
EN

Stack Overflow用户

发布于 2017-10-17 01:55:28

swift 4版本:

代码语言:javascript
复制
var monitorcontroler: NSWindowController!

override init() {
    super.init()

    let mainStoryboard = NSStoryboard.init(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
    monitorcontroler = mainStoryboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "moniteur")) as! NSWindowController
    monitorcontroler.showWindow(self)
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26689699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档