OS X + Swift + Xcode : Programmatically transition from one view controller to another

Hello,

I have a swift program that allows me to transition from a primary view controller to a secondary view controller. To complete this task I create a segue between a button on the primary view controller and the secondary view controller by dragging from the button to the secondary view controller then assigning an identifier to this seque. The balance is handled programatically.

What I would like to do is eliminate the creation of the segue via dragging and handle transitioning to the secondary view controller 100% in code. I have given the secondary view controller a storyboard id so it seems logical to me that I should be able to locate the secondary view controller via this identifier and use this to transition to it.

Is there a way to do this?

Regards,

Chris

You can instantiate the target view controller through the storyboard, and use any of the presentation method like present(_:animator:).

Code in the IBAction should be like:

let storyBoard = NSStoryboard(name: "Main", bundle: nil) as NSStoryboard
let secondController = storyBoard.instantiateController(withIdentifier:  "secondVC")
            self.presentAsModalWindow(secondController, animated: true, completion: nil)

Don't forget to remove the segue from the button.

        if let primaryController = self.storyboard?.instantiateController(withIdentifier: "ViewController") as? NSViewController {

            self.view.window?.contentViewController = primaryController

        }

This code appears to work. Thank you for the input.

Chris

OS X + Swift + Xcode : Programmatically transition from one view controller to another
 
 
Q