Hi,
i've a problem with the following code:
When I klick on a Button in my List, I go to another view:
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?
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?