Lifecycle of a UIViewController - App Development with Swift training

I'm working through the App Development with Swift, iOS 12 Edition and have run into a 'bug' in the Personality quiz at the end of Unit 3. I've also downloaded the 'solution' from the teachers resources and I'm seeing the same experience.


I believe the issue is in the lifecycle of a UIViewController and the variables that are initialized at creation. The book suggests that an index is created in the "QuestionViewController" which is a UIViewController, and the index works fine on the first pass of the app. The app consists of three view controllers, one 'intro', one 'question' and one 'results'. The intro controller has a simple seque to the 'question' controller and doesn't pass in any information. The 'question' controller presents the user with a list of questions that are then passed to the 'results' controller after all questions have been answered, a tally is caculated, and the results are displayed. The 'results' controller also has a UIBarButtonItem as a 'Done' button that is control-dragged to the exit that goes to a function in the 'intro' controller called "unWindToQuizIntroduction" that has no logic built in. I believe the expectation is that when the app is returned to the 'intro' controller, the 'question' controller has been eliminated and would be recreated on the segue to the (new) 'question' controler.


However, it seems that the old 'question' controller is retained between segues as the index value that had been incremented is still the same value, which ends in an 'out of range' error when calling the question controller a second time. As I said before, I compared 'my' code from the textbook against the project that was in the teachers manual, and I noticed two differences. First, the index is created in a different swift file that also contains the question and answer arrays. That shouldn't matter but I moved anyhow and results are the same. Secondly, the 'intro' controller in the teachers manual has an overridden function 'didReceiveMemoryWarning' which also doesn't modify behavior.


So the overall question is regarding the lifecycle of a viewController and the expectation of those values to be reinitialized.


The project can be found in this zipfile under Unit 3->Personality Quiz

http://education-static.apple.com/app-dev-swift/v2.7/resources/teacher.zip

Replies

You don't formulate a clear question but already give your anwer.


Very good, but then there is limited interest to post on the forum, isn't it ?


So the overall question is regarding the lifecycle of a viewController and the expectation of those values to be reinitialized.

But what is the precise question ?

Thanks for the request for clarification.


Precise question: Why is the index retaining the prior value instead of being reinitialized to 0?

I downloaded, but there are a lot of files to search through.


Which one exactly ? I do not find Unit 3->Personality Quiz.


And more:

- what do you expect exactly ? Is it this ?

I believe the expectation is that when the app is returned to the 'intro' controller, the 'question' controller has been eliminated and would be recreated on the segue to the (new) 'question' controler.

If so, why do you expect so ?

May be that is not the app designer intent !


If that's what you want, what did you try to change to get it working as you'd like ?


Please show the aact part of code and what you tried to change.

Kvy

File is located in Teacher Resources -> Navigation and Workflows->Guided Projects-Quiz->PersonalityQuiz.


Regarding the designer's intent:

“The third view controller tallies up the answers and presents the final outcome. This result can be dismissed, allowing another player to start the quiz from the first view controller.”

Excerpt From: Apple Education. “App Development with Swift.” Apple Inc. - Education, 2019. Apple Books. https://books.apple.com/us/book/app-development-with-swift/id1465002990


Additionally, the intent is implied with the logic of the done button and the expectation of what the 'begin personality quiz' button would do.


Regarding your question about the specific coding element: this is the root of my question. The expectation set by the text in the book is that the second view controller would be deinitialized at 'done' and subsequently reinitialized after clicking on the 'begin personality quiz' button on the introduction view controller. Given that, there isn't a specific element of code that I could site. Instead I'm attempting to understand how the viewControllers are deinitalized or reinitialized with swift by posting my question in this forum!


Again, thanks for your help!

I did not look at the details of the exercise.


But normally, you should simply:

- In the first viewController (which animal are you), create an IBAction for unwind Segue:

    @IBAction func unwindToStartViewController(_ sender: UIStoryboardSegue) {
          // No code needed
    }

- control drag from the Done button to the small exit icon at the top of its view controller (the rightmost icon with an exit sign)

- select the unwind action : unwindToStartViewController


That should be enough (once again if I interpreted the exercice correctly)


That's perfectly explained page 444

Did you finally solve it ?