Update per Frame Swift UI

Hi,
i've a problem with the following code:
Code Block language
import SwiftUI
struct ContentView: View {
   
 
   
  static var listItems = [Item]()
   
  var body: some View {
    NavigationView{
       
        List{
          ForEach(ContentView.listItems){ item in
            Text(item.name)
               
               
          }
          NavigationLink(
            destination: AddView(),
            label: {
              Text("                 Add")
                .foregroundColor(.blue)
            })
        }
       
         
    .navigationBarTitle("MyApp")
    }
     
  }
}
struct Item : Identifiable{
  var id = UUID()
  var name : String
  var image : String
   
}
struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

When I klick on a Button in my List, I go to another view:
Code Block language
import SwiftUI
struct AddView: View {
  @State var text:String = ""
  var body: some View {
     
    VStack{
      TextField("Name", text: $text).padding().background(Color(.systemGray5)).frame(width: 400,alignment: .center).cornerRadius(20)
       
       
       
      Button(action: {
        ContentView.listItems.append(Item(name: text, image: "Gif"))
        print(ContentView.listItems)
         
         
         
      }, label: {
        Text("Add")
      })
    }
  }
}
struct AddView_Previews: PreviewProvider {
  static var previews: some View {
    AddView()
  }
}

But when I klick on add in the AddView() and I go back to the First View nothing i changing. Can you help me please?


You need to change listItems to
Code Block Swift
@State private var listItems = [Item]()

and then in AddView add this property
Code Block Swift
@Binding var listItems: [Item]

By doing this you’re passing in the items array to AddView so in ContentView change the NavigationLink destination to
Code Block Swift
destination: AddView(listItems: $listItems)

Now AddView can directly modify the array which SwiftUI notices and reloads the body property to reflect the changes.
Thank you!

Update per Frame Swift UI
 
 
Q