为什么我的 ObservableObject 似乎被特定视图破坏了?

Why my ObservableObject seems to be broken with a specific view?

我创建了一个 class 用于将跟踪保持在实际视图中(一个简单的 ObservableObject 字符串):


import Foundation
import SwiftUI
import Combine

class ViewRouter: ObservableObject {

    @Published var currentView: String

    init() {
        self.currentView = "Home"
    }
}

我创建了一个简单的自定义 TabView,它与此配合得很好:

import SwiftUI

struct Test2CustomTabView: View {

    @ObservedObject var viewRouter = ViewRouter()

    var body: some View {
        GeometryReader { geometry in
            VStack {
                Spacer()
                if self.viewRouter.currentView == "Home" {
                    Home()
                } else if self.viewRouter.currentView == "Messages" {
                    Messages()
                }

                Spacer()
                HStack {

                    VStack {
                        Image(systemName: "house")
                        Text("Home")
                            .font(.caption)
                    }.frame(width: geometry.size.width/2, alignment: .center)
                        .offset(y: -15)
                        .onTapGesture {
                            self.viewRouter.currentView = "Home"
                    }

                    VStack {
                        Image(systemName: "envelope")
                        Text("Messages")
                            .font(.caption)
                    }.frame(width: geometry.size.width/2, alignment: .center)
                        .offset(y: -15)
                        .onTapGesture {
                            self.viewRouter.currentView = "Messages"
                    }

                }.frame(height: 85)
                    .foregroundColor(.white)
                    .background(Color(#colorLiteral(red: 0.259467423, green: 0.5342320204, blue: 0.7349982858, alpha: 1))) // On utilise ici extension Color plus bas

            }
        }
        .edgesIgnoringSafeArea(.bottom)
    }
}

我想简化代码,所以我创建了这个视图:

import SwiftUI

struct Test2CustomTabViewItem: View {
    @ObservedObject var viewRouter = ViewRouter()
    var systemName:String
    var viewName:String

    let screenSize: CGRect = UIScreen.main.bounds

    var body: some View {
        VStack {
            Image(systemName: systemName)
            Text("Home")
                .font(.caption)
        }.frame(width: screenSize.width/2, alignment: .center)
            .offset(y: -15)
            .onTapGesture {
                self.viewRouter.currentView = self.viewName
        }
    }
}

struct Test2CustomTabViewItem_Previews: PreviewProvider {
    static var previews: some View {
        Test2CustomTabViewItem(systemName: "house", viewName: "Home")
    }
}

现在我像这样使用 Test2CustomTabView :

import SwiftUI

struct Test2CustomTabView: View {

    @ObservedObject var viewRouter = ViewRouter()

    var body: some View {
        GeometryReader { geometry in
            VStack {
                Spacer()
                if self.viewRouter.currentView == "Home" {
                    Home()
                } else if self.viewRouter.currentView == "Messages" {
                    Messages()
                }

                Spacer()
                HStack {
                   Test2CustomTabViewItem(systemName: "house", viewName: "Home")
                   Test2CustomTabViewItem(systemName: "envelope", viewName: "Messages")

                }.frame(height: 85)
                    .foregroundColor(.white)
                    .background(Color(#colorLiteral(red: 0.259467423, green: 0.5342320204, blue: 0.7349982858, alpha: 1))) // On utilise ici extension Color plus bas

            }
        }
        .edgesIgnoringSafeArea(.bottom)
    }
}

struct Test2CustomTabView_Previews: PreviewProvider {
    static var previews: some View {
        Test2CustomTabView()
    }
}


但是现在当我点击自定义 tabview 项时,视图没有刷新,ObservableObject 中的更改不会刷新视图,我不明白为什么..

有什么想法吗?

谢谢

在您的代码中,每个视图都有自己的 ViewRouter 实例。将其从构造函数传递给 Test2CustomTabViewItem 视图或作为 environmentObject:

struct Test2CustomTabViewItem: View {
    @ObservedObject var viewRouter: ViewRouter
    var systemName:String
    var viewName:String
    ...
}

struct Test2CustomTabView: View {
    @ObservedObject var viewRouter = ViewRouter()
    ...
        Test2CustomTabViewItem(viewRouter: self.viewRouter, systemName: "house", viewName: "Home")
        Test2CustomTabViewItem(viewRouter: self.viewRouter, systemName: "envelope", viewName: "Messages")
    ...
}