我在Xcode6中創建了一個使用故事板的Cocoa應用程序。作爲一個模板,Xcode爲應用程序提供了一個窗口。我想添加第二個窗口來顯示程序何時第一次加載。所以基本上,會出現兩個窗口。使用故事板爲OS X初始化另一個窗口
我在Main.storyboard上放置了一個窗口控制器,第一個窗口也駐留在這裏。但是,我無法找到程序啓動時顯示第二個窗口的方式。能否請你幫忙?
謝謝。
我在Xcode6中創建了一個使用故事板的Cocoa應用程序。作爲一個模板,Xcode爲應用程序提供了一個窗口。我想添加第二個窗口來顯示程序何時第一次加載。所以基本上,會出現兩個窗口。使用故事板爲OS X初始化另一個窗口
我在Main.storyboard上放置了一個窗口控制器,第一個窗口也駐留在這裏。但是,我無法找到程序啓動時顯示第二個窗口的方式。能否請你幫忙?
謝謝。
在你的故事板中,選擇你的第二個窗口控制器。在身份檢查,指定此窗口控制器的名稱,如secondWindowController
然後,在你的應用程序委託,設立窗口控制器屬性:
@property NSWindowController *myController;
在您的applicationDidFinishLaunching:方法的實現,創建對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
斯威夫特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
的功能外,否則窗口將不會出現。
實際上,在init()(AppDelegate)中執行此操作會更好,因爲您可能需要它。
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)
}
謝謝!我還必須將「Storyboard ID」設置爲「Main」,並且工作正常。 – Hakan 2014-12-03 18:42:40
''[storyBoard instantiateInitialController]'''可以繞過ID問題(只要你已經設置了你想要的窗口,作爲故事板文件中的初始視圖控制器。 – Supertecnoboff 2017-06-27 21:27:10