【问题标题】:SwiftUI: Update ObservableObject array in Detail TabView to update Main ListViewSwiftUI:更新 Detail TabView 中的 ObservableObject 数组以更新 Main ListView
【发布时间】:2020-12-18 21:07:25
【问题描述】:

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

我有四个“开关”行的主列表视图,每个行都有一个switchTask 属性。详细视图是一个带有几个屏幕的 Tabview。当用户在任何详细选项卡视图中更新 switchTask 时,我需要在主列表视图中为相应的 SwitchRow 更新该任务属性。

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

ObservableObject 和 Row:

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

详细视图:(当前硬编码数组元素以更新 Switch Input 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")
                   }
                }
            }
        }

【问题讨论】:

    标签: arrays listview swiftui tabview observableobject


    【解决方案1】:
    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)
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-04
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多