tiamat tiamat - 5 months ago 9
iOS Question

opening ViewController raise unexpectedly found nil while unwrapping an Optional value

I'm trying to open a new ViewController from my code by using

let registrationView = NewOrdoViewController()
self.presentViewController(registrationView, animated: true, completion: nil)


my NewOrdoViewController is containing a ScrollView and while opening the new View, my app crash at the following line:

override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentSize.height = 2000 <-- it crash here
......


with the following error:

unexpectedly found nil while unwrapping an Optional value


is there any specific command to instantiate a new ViewController containing a ScrollView ?

Answer

You're opening a generic NewOrdoViewController, not the instance that exists in your storyboard. Give your NewOrdoViewController a storyboard ID in the interface builder and replace

let registrationView = NewOrdoViewController()

with

let registrationView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("YourStoryboardID") as! NewOrdoViewController