|
How do I capture data from a TextField and essentially "store it" for display in another view?
And I am still getting errors and more confused than ever! FILE ONE CALLED QuizSettings.swift
FILE TWO CALLED WelcomeView (this is where we capture user input via the text field)
FILE THREE CALLED HeaderView (this is where I want whatever the user entered on WelcomeView, to be displayed)
Also keep in mind each of the 3 files above have a "Preview" section that ALSO has to be free of errors. (Which also need their own State/Observed/Object variables??) If you know how to fix my issue, please include "the fix" for these Preview sections too? The specific error thrown is about not finding the EnvironmentObject Ancestor View. . . Thread 1: Fatal error: No ObservableObject of type QuizSettings found. A View.environmentObject(_:) for QuizSettings may be missing as an ancestor of this view. |
|
Create a new XCode application for iOS. Paste this code. Study each comment. Please come back here and tell us what you learned about Observed and Observable objects.
|
|
This is another example:
You don't need previews. You can leave them out, comment them out, or delete them entirely. However, here are the previews for my code:
|
|
Hello! I am here to follow-up as requested with "what I've learned."
The only other note to add for someone else following these footsteps, know that to really undestand this example, I put each View in it's own swift file. The "default code" for each file's preview worked fine EXCEPT for the WizardView file. To clear these errors, I had to pass Wizard.init() to the student parameter as follows:
Again, all the credit due to @Obelix! I will now try to take these lessons into my original project :) |
|
I enjoyed Vince's addition! He also showed how you can pass an Observed object to a view struct, but the view struct can appear in a different screen. (Via Navigation views.) This moves more towards your original question. If you think of the lego brick, I just put two of them on the same screen as the TextField. But you can create a Lego brick on a second screen and navigate TO that screen. But the same rules apply! You can't create the lego brick anywhere, unless you provide an appropriate object for that brick to display. Glad this helped! |
|
In this post Scaffolding I describe setting up fake data for a PreviewProvider. You craft a lego block, such as You did this nicely with the In that case, you may have to create a fake object with a proper initializer before you can pass it to your PreviewProvider struct.
This gets trickier when you want your preview to display an array of objects from a CoreData database. Or from a live json feed! In those cases, you may want to consider NOT using previews. If you're a glutton, others have posted deep dive solutions. So to follow his excellent guidance, we might define the Wizard class like this.
Then you can simplify the preview code like this:
If you ALWAYS use |
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.