I created an OS X Cocoa Application in Xcode 7.2.1. I gave the default view controller on Main.storyboard a Storyboard ID of "view1". I added another view controller and gave it a Storyboard ID of "view2". I added a button to "view1" and linked the action to ViewController.swift and added the following code:
@IBAction func view1Button(sender: AnyObject) {
for v in view.subviews {
v.removeFromSuperview()
}
let mainStoryboard: NSStoryboard = NSStoryboard(name: "Main", bundle: nil)
let sourceViewController = mainStoryboard.instantiateControllerWithIdentifier("view2")
self.view.addSubview(sourceViewController.view)
}
When I press the button on "view1", everything works as expected. "view1" is dismissed and "view2" appears within the same window.
On "view2", I did the same thing. Added a button and linked the action to ViewController2.swift and added the following code:
@IBAction func view2Button(sender: AnyObject) {
for v in view.subviews {
v.removeFromSuperview()
}
let mainStoryboard: NSStoryboard = NSStoryboard(name: "Main", bundle: nil)
let sourceViewController = mainStoryboard.instantiateControllerWithIdentifier("view1")
self.view.addSubview(sourceViewController.view)
}
It does not work as expected. When I click the button on "view2" the app crashes and I receive the following error: "Thread 1: EXC_BAD_ACCESS (code=1, address=...). I don't understand why this isn't working as expected?