Cocoa:根据App Delegate

时间:2016-04-04 13:02:59

标签: swift macos appdelegate

我试图通过AppDelegate触发2个不同流中的1个,但似乎正在挣扎。

见下图:

Storyboard with 2 views 如果只有一个可以绑定到主NSWindow控制器中,如何实例化这两个不同的视图控制器?

我设法使用" addChildWindow"来触发它们。使用以下代码 - 但后来我无法转向任何后续的视图控制器。

以下是我用来实例化视图控制器的代码:

   if let pboardString = pboard.stringForType(NSStringPboardType){
            print(pboardString)
           let storyboard = NSStoryboard(name: "Main", bundle: nil)
            let destcontroller  =  storyboard.instantiateControllerWithIdentifier("pickProject") as! WizardTextFromPasteboardVC
            destcontroller.weburlFromPaste = pboardString

            let numWindows = NSApplication.sharedApplication().windows
            if let window = NSApplication.sharedApplication().mainWindow {
                if numWindows.count >= 1{
                    let childWindow = NSWindow(contentViewController: destcontroller)
                    window.addChildWindow(childWindow, ordered: .Above)
                }
                else if numWindows.count == 0 {
                    window.contentViewController = destcontroller
                }

1 个答案:

答案 0 :(得分:0)

没有必要保留提供窗口内容的默认segue。如果删除它并提供两个可以通过id实例化的视图控制器,那么您的应用程序委托可以决定何时启动完成。

如:

let selection: Int = 2

func applicationDidFinishLaunching(aNotification: NSNotification) {
    let storyboard = NSStoryboard(name: "Main", bundle: nil)
    var controller: NSViewController
    if selection == 1 {
        controller = storyboard.instantiateControllerWithIdentifier("view_controller") as! NSViewController
    } else {
        controller = storyboard.instantiateControllerWithIdentifier("view_controller_2") as! NSViewController
    }
    let window = NSApp.windows[0]
    window.contentViewController = controller
}