0

I have created a second View Controller in the main storyboard with the same view controller class. I created an outlet for this view.

If a button was clicked, I want to add this second view over the first view. How can I call this second view?

Code of secondViewController:

@IBOutlet weak var secondView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.view = secondView
}

override func loadView() {
    super.loadView()
    //  Fatal error: Unexpectedly found nil while unwrapping an Optional value
    self.view.addSubview(informationView)
}

Code of firstViewController, where I call the secondView and put it in a plane:

       let secondViewController:UIViewController = SecondViewController()
       let plane = SCNPlane(width: CGFloat(0.1), height: CGFloat(0.1))
       plane.firstMaterial?.diffuse.contents = secondViewController.view

I only get a white screen, also the background color is not shown.

Informatics
  • 155
  • 1
  • 10
  • Do u want secondViewcontroller to be inside in firstViewController?? or, move from firstViewController to secondViewcontroller ?/ – McDonal_11 Feb 13 '18 at 09:37
  • Possible duplicate of https://stackoverflow.com/questions/37771001/dismiss-and-present-view-controller-in-swift – Prashant Tukadiya Feb 13 '18 at 09:37
  • Yes, I called in firstViewController the secondViewController. But then the view of the secondViewController is not shown, it is white. So how can I load the second view over the first? – Informatics Feb 13 '18 at 09:41
  • Not sure what you're trying to achieve but there a few issues in your code here: 1) you don't assign self.view in viewDidLoad (too late - do it in loadView if needed) 2) you must not call super.loadView() (check the documentation) 3) Yes you get a fatal error because the view is not created yet in loadView - you only override this guy if you want to assign self.view yourself. You should add the subview in viewDidLoad instead. – tanz Feb 13 '18 at 10:16
  • @tanzolone Thanks a lot! If I add addSubview to viewDidLoad I get the following error: Thread 1: EXC_BAD_ACCESS (code=2, address=0x16ef83fe0) – Informatics Feb 13 '18 at 10:25
  • If you've created the view controller in storyboard you should instantiate it with `storyboard.instantiateViewController(withIdentifier:)` or `storyboard.instantiateInitialViewController()` not with a plain `init()`. – Lësha Turkowski Feb 13 '18 at 11:24
  • 1) If you don't assign a view to `self.view` you shouldn't override loadView at all - if you override but don't do it then your view will never be initialized. This could be a reason for the bad access. 2) The other reason could be that `informationView` is a not properly instantiated object - I can't see from your snippet where it comes from. – tanz Feb 13 '18 at 16:16

1 Answers1

0

1: You should set a storyboard id for your view controller like here

2: Then you should instantiate it like this:

guard let secondViewController = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as? MyViewControllerClass else { return }

And then do everything you want with your secondViewController.

I. Pedan
  • 244
  • 2
  • 9