Avatar
omochimetaru 4/30/2023 2:18 AM
あ〜関係ありそう・・・!
2:19 AM
https://developer.apple.com/documentation/swiftui/stateobject class DataModel: ObservableObject { @Published var name = "Some Name" @Published var isEnabled = false } struct MyView: View { @StateObject private var model = DataModel() // Create the state object. var body: some View { Text(model.name) // Updates when the data model changes. MySubView() .environmentObject(model) } }
2:19 AM
↑こういう書き方がお作法なんですね。
2:22 AM
struct TextView: View { @StateObject var viewModel = ViewModel() var body: some View { Button { viewModel.count += 1 } label: { Text("count: \(viewModel.count)") } } } ↑こう書き換えてみましたが、現象には変化無かったです。
t_naruhodo 1