Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

ios - Swift - Update List from different View

I have 2 Views in my Swift Project and when I click on the Button on the secondView, I want to update the List in the First View. I don't know how to do it! If I use a static variable in my MainView and then edit this variable from the secondView, it works, but it won't update. And if I don't use static and instead use @State, it would update, but I can't access it from my secondView.

Here is the Code below:

import SwiftUI

struct ContentView: View {
    
    var body: some View {

        TabView {
         
            MainView()
                .tabItem() {
                    VStack {
                        Image(systemName: "circle.fill")
                        Text("MainView")
                    }
                }.tag(0)
            
            UpdateOtherViewFromHere()
                .tabItem() {
                    VStack {
                        Image(systemName: "circle.fill")
                        Text("SecondView")
                    }
                }.tag(1)
        }
    
    
    }
}


struct MainView: View {
    
    var arrayList: [CreateListItems] = []
    
    init() {
        let a = CreateListItems(name: "First Name!")
        let b = CreateListItems(name: "Second Name!")
        let c = CreateListItems(name: "Third Name!")

        arrayList.append(a)
        arrayList.append(b)
        arrayList.append(c)
        
    }
    
    var body: some View {
        return VStack {
            ZStack {
            NavigationView {
                
                List {
                    ForEach(arrayList) { x in
                        Text("(x.name)")
                    }
                }.navigationBarTitle("Main View")
   
            }

        }
        }
    }
}

struct UpdateOtherViewFromHere: View {
    
    func updateList() {
        //Code that should remove "FirstName" from the List in MainView
    }
    
    var body: some View {
        return VStack {
            Button(action: {
                updateList()
            }) {
                Image(systemName: "heart.slash")
                    .font(.largeTitle)
                    

                Text("Click Me!")
            }
        }
    }
}


struct CreateListItems: Identifiable {
    var id: UUID = UUID()
    var name: String
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can share it using @State and @Binding if you put

struct ContentView: View {
     @State var arrayList: [CreateListItems] = []

struct MainView: View {
     @Binding var arrayList: [CreateListItems]

struct UpdateOtherViewFromHere: View {
     @Binding var arrayList: [CreateListItems]

or you use the MVVM pattern and store the list in an ObservableObject and use @StateObject/@ObservedObject (source) and use @EnvironmentObject(connection) to share it between your Views.

https://developer.apple.com/documentation/swiftui/managing-model-data-in-your-app

class ParentViewModel: ObservableObject{
    @Published var arrayList: [CreateListItems] = []
    
    init(){
        addSamples()
    }
    func addSamples()  {
        let a = CreateListItems(name: "First Name!")
        let b = CreateListItems(name: "Second Name!")
        let c = CreateListItems(name: "Third Name!")
        
        arrayList.append(a)
        arrayList.append(b)
        arrayList.append(c)
    }
    func updateList() {
        let a = CreateListItems(name: "(arrayList.count + 1) Name!")
        arrayList.append(a)
    }
}
struct ParentView: View {
    @StateObject var vm: ParentViewModel = ParentViewModel()
    var body: some View {
        
        TabView {
            MainView().environmentObject(vm)
                .tabItem() {
                    VStack {
                        Image(systemName: "circle.fill")
                        Text("MainView")
                    }
                }.tag(0)
            
            UpdateOtherViewFromHere().environmentObject(vm)
                .tabItem() {
                    VStack {
                        Image(systemName: "circle.fill")
                        Text("SecondView")
                    }
                }.tag(1)
        }
    }
}
struct MainView: View {
    @EnvironmentObject var vm: ParentViewModel
    
    var body: some View {
        return VStack {
            ZStack {
                NavigationView {
                    
                    List {
                        ForEach(vm.arrayList) { x in
                            Text(x.name)
                        }
                    }.navigationBarTitle("Main View")
                }
            }
        }
    }
}
    
struct UpdateOtherViewFromHere: View {
    @EnvironmentObject var vm: ParentViewModel
    var body: some View {
        return VStack {
            Button(action: {
                vm.updateList()
            }) {
                Image(systemName: "heart.slash")
                    .font(.largeTitle)

                Text("Click Me!")
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...