0

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?

user1822824
  • 2,478
  • 6
  • 41
  • 65

2 Answers2

0

I suspect the reason for your crash is that there is no strong reference to sourceViewController (which you instantiate in view2Button(_:)). You add its view as a subview, which keeps the view in memory, but once the action method returns there will be no strong reference to the view controller, so when the action message is sent from the button there is no view controller there to receive it.

Stepping back from the specifics of this code, I would suggest doing some research - what you are trying to implement is called "view swapping" in Cocoa Programming for OS X; in iOS it is "view controller containment". It generally works out much better if there is a parent view controller that does the swapping. If you have Cocoa Programming for OS X 5th Edition handy, have a look at Chapter 31. But if you don't, you can also look over the sample code for that chapter. Still, I would suggest looking at the book (disclaimer: I'm a coauthor on it).

Adam Preble
  • 2,162
  • 17
  • 28
  • Hey Adain, take appear above... I'm just trying to work out if you're using "Storyboards" where you have one fie (something.storyboard) with a load of views on it, of several files, each being separate view?... Each scenario is different.. – Adrian Sluyters Mar 17 '16 at 04:23
  • @AdrianSluyters They have their differences, definitely, but once you have a reference to the view controller (whether you loaded it and its view out of a storyboard, or if you instantiated the view controller and loaded its view out of a XIB), the principles of swapping the view and maintaining a reference to the view controller so it stays in memory are the same. – Adam Preble Mar 17 '16 at 04:38
  • Where a few differences with the newer Xcode releases and controller "references".. They can be either "single" or "multiply" created, so things vary.. Pop me a quick chat and I'll explain and explain the differences between the two so you get to choose :) – Adrian Sluyters Mar 17 '16 at 04:40
  • HIs problem is when he's dismissing the view from one to call the other, the VC is deinit-ed by background collection... – Adrian Sluyters Mar 17 '16 at 10:01
0

Just to clarify are you using Xibs (or Nibs) or even storyboards?..

If you're using storyboards, things work slightly differently than to the individual controllers... They individually load on their own accord (in their expected order), and need to be addressed accordingly.

So if using storyboards (which I think you are as I had the same in the beginning, let us know).

It all is different depending on how your controllers arte arranged...

Storyboards appear like this (for example):

enter image description here

See the links etc.. between the views?

And the rest... Appear like this (the xibs):

enter image description here

Adrian Sluyters
  • 2,186
  • 1
  • 16
  • 21
  • I am using storyboards, no XIB files. – user1822824 Mar 17 '16 at 04:21
  • I've slapped together a real simple example here. One project, two targets. One target is `codeless` i.e. using the built in segue's (transitions) between views. and the other one you manually call the transition... https://dl.dropboxusercontent.com/u/61211034/Stackoverflow/ToFromTwoViews.zip – Adrian Sluyters Mar 17 '16 at 10:03
  • Hope that helps and clarifies where we're all at :D That's all Storyboard based, so should be what you're looking for.. – Adrian Sluyters Mar 17 '16 at 10:03
  • Thanks. I will take a look. I was able to fix the issue by slightly modifying the code that I posted. The app now works as expected and no longer crashes when going back and forth between views. – user1822824 Mar 17 '16 at 21:40