1

我创建了一个类,用于将跟踪保留为实际视图(一个简单的 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 中的更改不会刷新视图,我不明白为什么..

任何想法?

谢谢

4

1 回答 1

2

在您的代码中,每个视图都有自己的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")
    ...
}
于 2020-05-12T18:12:59.827 回答