tiamat tiamat - 1 year ago 64
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() {
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 Source

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()


let registrationView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("YourStoryboardID") as! NewOrdoViewController
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download