我已经在Xcode6中创建了一个使用故事板的Cocoa应用程序。作为模板,Xcode为应用程序提供了一个窗口。我想添加第二个窗口来显示程序第一次加载的时间。所以基本上,会出现两个窗口。
我在Main.storyboard上放置了一个窗口控制器,第一个窗口也驻留在那里。但是,我找不到在程序启动时显示第二个窗口的方法。你能帮帮忙吗?
谢谢。
发布于 2014-11-29 19:52:33
在故事板中,选择第二个窗口控制器。在标识检查器中,指定此窗口控制器的名称,例如secondWindowController
然后,在您的应用程序委托中,为窗口控制器设置一个属性:
@property NSWindowController *myController;
在您的applicationDidFinishLaunching: method实现中,创建对Storyboard的引用。这样你就可以从故事板访问你的窗口控制器了。在此之后,剩下的唯一要做的就是通过向窗口控制器发送showWindow:方法来显示窗口。
#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
发布于 2017-03-11 23:21:23
Swift 3版本:
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()中做这件事会更好,因为您可能会在前面需要它。
发布于 2017-10-17 01:55:28
swift 4版本:
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)
}
https://stackoverflow.com/questions/26689699
复制相似问题