SwiftUI在视图之间共享模型数据

SwiftUI在视图之间共享模型数据,swift,swiftui,Swift,Swiftui,我有这样一个结构,这个结构在我看来是错误的,因为我想问你的方法。因此,当我想在一个视图中使用两个模型时,我必须在一个视图中将其放在foreach中。这就是我想要的。在我想要的其他页面上使用我在用户配置文件中使用的数据。我该怎么做?你们是怎么做到的 让我举个例子让你更好地理解: 我想在主页上显示我的用户的用户名数据,我应该怎么做?。事实上,在初始化一次模型之后,我想在其他视图中使用它。什么是正确的方法 import SwiftUI struct ContentView: View {

我有这样一个结构,这个结构在我看来是错误的,因为我想问你的方法。因此,当我想在一个视图中使用两个模型时,我必须在一个视图中将其放在foreach中。这就是我想要的。在我想要的其他页面上使用我在用户配置文件中使用的数据。我该怎么做?你们是怎么做到的

让我举个例子让你更好地理解:

我想在主页上显示我的用户的用户名数据,我应该怎么做?。事实上,在初始化一次模型之后,我想在其他视图中使用它。什么是正确的方法

  import SwiftUI

struct ContentView: View {
    @StateObject var network = ProfileNetwork()
    var body: some View {
        TabView{
            ProfileView().tabItem { Image(systemName: "house") }
            ForEach(self.network.userprofile,id:\.id){a in
                ShopView(profile_model: a)
            }.tabItem { Image(systemName: "house") }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


class ProfileNetwork : ObservableObject {
    @Published var userprofile : [UserPRofile] = [UserPRofile(name: "Test", coin: 1, id: "dsa")]
}

struct ProfileView : View {
    @StateObject var network = ProfileNetwork()
    var body: some View {
        ForEach(self.network.userprofile, id:\.id){ i in
            ProfileViewModel(profile_model: i)
        }
    }
}

struct ProfileViewModel : View {
    var profile_model : UserPRofile
    var body: some View {
        Text(self.profile_model.name)
    }
}

struct UserPRofile : Decodable{
    var name : String
    var coin : Int
    var id : String
}

class ShopeNetwork : ObservableObject {
    @Published var shop : [ShopStore] = [ShopStore(id: "sda", image: "dasd", price: 100, name: "sda")]
}

struct ShopView : View {
    @StateObject var network = ShopeNetwork()
    var profile_model : UserPRofile
    var body: some View {
        ForEach(self.network.shop, id:\.id){ c in
            ShopViewModel(shop_model: c, profile_model: profile_model)
        }
    }
}

struct ShopViewModel : View {
    var shop_model : ShopStore
    var profile_model : UserPRofile
    var body: some View {
        Text(profile_model.name)
        Text(self.shop_model.name)
    }
}


struct ShopStore : Decodable {
    var id : String
    var image : String
    var price : Int
    var name : String
}

一种可能的解决方案是创建一个
@EnvironmentObject
,并将其注入根级别:

class AppState: ObservableObject {
    @Published var userProfile: UserPRofile?
}
Swift 5,iOS 14

使该类成为一个单例类,使其易于共享

class ProfileNetwork : ObservableObject {
    @Published var userprofile : [UserPRofile] = [UserPRofile(name: "Test", coin: 1, id: "dsa")]
    static var shared = ProfileNetwork()
}
然后用共享句柄引用它

struct ContentView: View {
  @StateObject var network = ProfileNetwork.shared
  var body: some View {

....
}

struct ProfileView : View {
  @StateObject var network = ProfileNetwork.shared 
  var body: some View {

....
}

这里主页的意义是什么?
struct ShopView: View {
    @EnvironmentObject private var appState: AppState // use it in any other view
    ...
class ProfileNetwork : ObservableObject {
    @Published var userprofile : [UserPRofile] = [UserPRofile(name: "Test", coin: 1, id: "dsa")]
    static var shared = ProfileNetwork()
}
struct ContentView: View {
  @StateObject var network = ProfileNetwork.shared
  var body: some View {

....
}

struct ProfileView : View {
  @StateObject var network = ProfileNetwork.shared 
  var body: some View {

....
}