Swift - 从不同的视图更新列表

Swift - Update List from different View

我的 Swift 项目中有 2 个视图,当我单击第二个视图上的按钮时,我想更新第一个视图中的列表。我不知道该怎么做!如果我在我的 MainView 中使用一个静态变量,然后从 secondView 编辑这个变量,它可以工作,但它不会更新。如果我不使用静态而是使用@State,它会更新,但我无法从我的 secondView 访问它。

下面是代码:

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()
    }
}

您可以使用 @State@Binding 分享它,如果您输入

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

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

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

或者您使用 MVVM 模式并将列表存储在 ObservableObject 中并使用 @StateObject/@ObservedObject(来源)并使用 @EnvironmentObject(连接)共享它在您的视图之间。

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!")
            }
        }
    }
}