InstantiateViewControllerWithIdentifier failed (nil found)

When I try instantiateViewControllerWithIdentifier

on iPhone it crashes the app, although it works fine on the ios simulator. The code I used is:

let questionsGameVC: QuestionsGame = self.storyboard?.instantiateViewControllerWithIdentifier("Questions") as! QuestionsGame

      

The error referred to is

fatal error: unexpectedly found nil while expanding an optional value

Can anyone add something to what's going wrong?

+3


source to share


1 answer


There are many places where this can go wrong. I would put a breakpoint on that line and see the system state, but swift debugging is still not great. One option is to break this line of code and check all parts. Something like that:

if let storyboard = self.storyboard {
    if let viewController = storyboard.instantiateViewControllerWithIdentifier("Questions") {
        if let questionGame = viewController as? QuestionGame {
            println("Success!")
        } else {
            println("Question game is the wrong type in the storyboard")
        }
    } else {
        println("There is no view controller with the identifier Questions")
    }
} else {
    println("The storyboard is nil")
}

      



Anything printed at the end should give you a better idea of ​​where the problem is. More often than not, I've seen erroneous IDs or situations where the view controller class in the storyboard hasn't been changed from UIViewController

to a custom one.

0


source







All Articles