SwiftUI:更新 Detail TabView 中的 ObservableObject 数组以更新 Main ListView

SwiftUI: Update ObservableObject array in Detail TabView to update Main ListView

我是编程和 SwiftUI 的新手,所以这将是基础知识。 (此代码是我发现的不同 ObservableObject 教程的组合,因此有 unnecessary/wrong 个步骤,事实上,整个事情可能是错误的)。非常感谢任何帮助!!

我有一个包含四 'switch' 行的主列表视图,每行有一个 switchTask 属性。详细视图是一个带有几个屏幕的 Tabview。当用户在任何详细信息选项卡视图中更新 switchTask 时,我需要该任务 属性 在主列表视图中更新相应的 SwitchRow

我的 ObservableObject 是一个数组 switches。我无法弄清楚如何正确地将每个 Switch 实例传递给 Tabview 详细信息 (PluggedInDetail),因此更新了主列表视图中相应的 switchTask。我应该改用 EnvironmentObject 吗?

ObservableObject 和行:

import SwiftUI
import Combine

class Switch: ObservableObject, Identifiable {

    let id = UUID()
    @Published var switchName: String
    @Published var switchTask: String

    init (switchName: String, switchTask: String) {
         self.switchName = switchName
         self.switchTask = switchTask
   }
}

class Switches: ObservableObject {

    //private init() { }
    static let shared = Switches()

    @Published var switches: [Switch]

    init() {
        //switchTask is to be updated on tabview detail
        self.switches = [
        Switch(switchName: "Switch Input 1", switchTask: ""),
        Switch(switchName: "Switch Input 2", switchTask: ""),
        Switch(switchName: "Switch Input 3", switchTask: ""),
        Switch(switchName: "Switch Input 4", switchTask: "")]
    }
}
struct SwitchRow: View {
  
    @ObservedObject var myswitch: Switch

    var body: some View {
        HStack{
            Image(systemName: "circle")
            VStack{
                Text(myswitch.switchName)
                Text(myswitch.switchTask)
           }
        }
    }
}

内容视图:

struct ContentView: View {

    @ObservedObject var myswitches: Switches = .shared

    var body: some View {
        NavigationView{
            VStack {
                List(Array(myswitches.switches.enumerated()), id: \.element.id) { (i, Switch) in
                    NavigationLink(destination: tabDetail()){
                        SwitchRow(myswitch: self.myswitches.switches[i])
                    }
                }
            }
        }
    }
}
struct tabDetail: View {

    @ObservedObject var detailSwitches: Switches = .shared

    var body: some View {
        TabView {
            PluggedInDetail()
                .tabItem {
                    Text("Plugged In")
            }

            TVDetail()
                .tabItem {
                    Text("TV")
            }
        }
    }
}

详细视图:(目前硬编码数组元素以更新开关输入 1。这是我需要帮助的地方)

import SwiftUI 
import Combine

struct PluggedInDetail: View {

    @ObservedObject var detailSwitches: Switches = .shared

    var body: some View {
        VStack {
            // switchName should display in the text
            Text(self.detailSwitches.switches[0].switchName)
            Button(action: {
                        //This should update switchTask for whichever switch element is selected
                        self.detailSwitches.switches[0].switchTask = "Direct Press"  
                    }) {
                        Text("Add Direct Press")
                   }
                }
            }
        }
struct SharedOO: View {
    @ObservedObject var myswitches: Switches = .shared
    
    var body: some View {
        NavigationView{
            VStack {
                List(myswitches.switches, id: \.id) { switche in
                    //You should only pass the individual item from here since the View is for a single Switch
                    NavigationLink(destination: SwitchDetail(switche: switche)){
                        SwitchRow(myswitch: switche)
                    }
                }
            }
        }
    }
}
struct SwitchDetail: View {
    @ObservedObject var switche: Switch
    
    var body: some View {
        TabView {
            PluggedInDetail().environmentObject(switche)
                .tabItem {
                    Text("Plugged In")
                }
            /*//No Code Provided
             TVDetail()
             .tabItem {
             Text("TV")
             }
             */
        }
    }
}
struct PluggedInDetail: View {
    
    @EnvironmentObject var switche: Switch
    
    var body: some View {
        VStack {
            // switchName should display in the text
            Text(self.switche.switchName)
            Text(self.switche.switchTask)
            Button(action: {
                //This should update switchTask for whichever switch element is selected
                self.switche.switchTask = "Direct Press"
            }) {
                Text("Add Direct Press")
            }
        }
    }
}
class Switch: ObservableObject, Identifiable {
    
    let id = UUID()
    @Published var switchName: String
    @Published var switchTask: String
    
    init (switchName: String, switchTask: String) {
        self.switchName = switchName
        self.switchTask = switchTask
    }
}

class Switches: ObservableObject {
    
    //private init() { }
    static let shared = Switches()
    
    @Published var switches: [Switch]
    
    init() {
        //switchTask is to be updated on tabview detail
        self.switches = [
            Switch(switchName: "Switch Input 1", switchTask: ""),
            Switch(switchName: "Switch Input 2", switchTask: ""),
            Switch(switchName: "Switch Input 3", switchTask: ""),
            Switch(switchName: "Switch Input 4", switchTask: "")]
    }
}
struct SwitchRow: View {
    
    @ObservedObject var myswitch: Switch
    
    var body: some View {
        HStack{
            Image(systemName: "circle")
            VStack{
                Text(myswitch.switchName)
                Text(myswitch.switchTask)
            }
        }
    }
}