Crash when I modify a bind value with a text field

Hi everyone,

I'm sorry to bother you, but I'm having a very annoying problem that I can't solve despite a lot of debugging time...

I will explain the context to you very quickly. I have a card list table (listes: [Liste]) in my ContentView page I do a foreach, and I pass one of the lists in binding in another view (AfficherUneListeView), and on this page I have a button which allows to modify the name of the list. When I'm on the edit list page and click on the textfield, my page "crashes" but not the app. So impossible to update the name (unless I'm very fast, I can modify a character and it spits (see the video to believe it).

https://youtu.be/9O-dATdAp2I

Here is the flow diagram of the list variable: ContentView -> then binding on AfficherUneListeView then Binding on ModifierUneListeVuew then Binding on TextField

I have attached all the files necessary to make it work, the code. I searched for hours, I followed tutorials... I can't understand I'm desperate, thank you in advance everyone!

Model structures (carte, listes, session) :

And the app :

Answered by Claude31 in 751273022

Well, I attach a file.

All changes are marked as ## (hope I marked them all).

Please ask if anything not clear or not working. Otherwise, complete the rest of your code using the same pattern (StateObject / EnvironmentObject / .environment(theObject) and don't forget to close the thread.

I'm sorry to bother you

You should not, that's the very purpose of the forum.

But we miss several struct to test:

  • SentrainerUIView
  • CreeUneListeView
  • ModifierUneCarte

You should try to run it in simulator instead of Preview.

If that does not work either, I would try to replace @State var listes with a @StateObject that I would pass to environment…

I attached the structs messing.

SentrainerUIView: is empty (no developed) CreeUneListeView : work ModifierUneCarte : the design is done, but the modification does not work (I am waiting for the modification of a list to work)

Thanks in advance :-)

It still misses CreerUneListe

This init in ModifierUneCarte is incorrect

    init(carte: Binding) {

It is probably

    init(carte: Binding<Carte>) {

It's really amazing, because in my code I have:

init(carte: Binding<Carte>) {

but when I put the file on the forum it disappears to put only "binding".... It's really very strange...

Sorry I attached you "CreerUneListeView"

Problem comes from

        TextField("Nom de la liste", text: $liste.nom)

If you replace (for test purpose)

            TextField("Nom de la liste", text: $listeNom)

where

@State var listeNom : String = "-"

Then you can tap in the TextField.

Problem comes (I think) from the Binding, which does not bind the property .nom

I succeeded to make it working (at least starts to) with environment var.

Mais le code est long. Si vous pouvez me donner une adresse où envoyer l'ensemble, je vous posterai les modifications.

Accepted Answer

Well, I attach a file.

All changes are marked as ## (hope I marked them all).

Please ask if anything not clear or not working. Otherwise, complete the rest of your code using the same pattern (StateObject / EnvironmentObject / .environment(theObject) and don't forget to close the thread.

Crash when I modify a bind value with a text field
 
 
Q