2

I am creating a simple quiz app in xcode that has an intro, question, and results view controller. When I try to link my "QuestionViewController" to my "ResultsViewController" with the prepare for segue method in code, the exclamation point in "as!" keyword is not highlighted in pink.. It gives me a "Thread 1: signal SIGABRT". Yes I have linked it in the storyboard previously.

I've tried checking the identifiers of the segues in the storyboard and they look fine. Here is the code I was talking about:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ResultsSegue" {
        let resultsViewController = segue.destination as! ResultsViewController
        resultsViewController.responses = answersChosen
    }
}

It is supposed to pass the data of answersChosen to my resultsViewController, but I am getting a "Thread 1: signal SIGABRT" error. And like I said, the "!" after "as" is not highlighted in pink like "as" is. In the iBook the "!" is pink.. that indicated to me something is not connected right.

Also in the console:

Could not cast value of type 'UINavigationController' (0x112186760) to 'CityQuiz.ResultsViewController' (0x10a61e698) 2019-07-01 14:45:35.043824-0400 CityQuiz[29761:429223] Could not cast value of type 'UINavigationController' (0x112186760) to 'CityQuiz.ResultsViewController' (0x10a61e698)

Isn't it supposed to say cast value of type "QuestionsViewController" instead of "UINavigationController" as well? How else do I check if things are linked properly besides actually clicking on the segues in the storyboard?

Edit #1: Turns out I had an extra navigation controller, that I somehow didn’t notice. I’m new to this, thanks for the responses. I am now getting some “NSUncaughtKeyException” but I found some other posts about those errors.

  • The error is telling you that `segue.destination` is actually a `UINavigationController` so the attempted cast to `ResultsViewController` is failing. – rmaddy Jul 01 '19 at 19:03

1 Answers1

0

why don't you present view controller programmatically ? It's much more simple than this method. I assume the problem you have in your case is that the segue "ResultsSegue" is attached to the embedded navigation controller. And you probably should not force unwrapped view controller like that. To check optional is a better way to handle presentation like below.

if let resultsViewController = segue.destination as? ResultsViewController { 
    resultsViewController.responses = answersChosen }
erkutbas
  • 305
  • 2
  • 9
  • Hmm I was just following the book and they said to do it this way, I'll have to check out the programmatic way. –  Jul 01 '19 at 19:32
  • It's really so simple :). you can check this quickly. https://stackoverflow.com/questions/39450124/swift-programmatically-navigate-to-another-view-controller-scene/39450328 – erkutbas Jul 01 '19 at 19:38