如何解决SwiftUI:更新 Detail TabView 中的 ObservableObject 数组以更新 Main ListView
我是编程和 SwiftUI 的新手,所以这将是基本的。 (此代码是我发现的不同 ObservableObject
教程的组合,因此存在不必要/错误的步骤,事实上,整个过程可能是错误的)。非常感谢任何帮助!!
我有一个包含四个“开关”行的主列表视图,每行都有一个 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",Switch(switchName: "Switch Input 3",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")
}
}
}
}
解决方法
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",Switch(switchName: "Switch Input 3",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)
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。