Post

Replies

Boosts

Views

Activity

Reply to can't use @Environment for Binding?
I also had that problem, this is the solution: add @Bindable var viewModel = viewModel in your view’s body, even though I don’t think it is elegant. @Observable final class View1Model { var text: String = "" } struct View1: View { @State var viewModel = View1Model() var body: some View { View2() .environment(viewModel) } } struct View2: View { @Environment(View1Model.self) var viewModel var body: some View { // use @Bindable to get the binding @Bindable var viewModel = viewModel TextField("Text", text: $viewModel.text) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’23
Reply to SwiftData preview sample code
on Xcode15 beta4, all workarounds above don’t work for me. I notice that release notes mentioned that it is a known issue and recommend to run on device replacing preview, but it is not convenient. is there any other workaround?
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Jul ’23
Reply to can't use @Environment for Binding?
I also had that problem, this is the solution: add @Bindable var viewModel = viewModel in your view’s body, even though I don’t think it is elegant. @Observable final class View1Model { var text: String = "" } struct View1: View { @State var viewModel = View1Model() var body: some View { View2() .environment(viewModel) } } struct View2: View { @Environment(View1Model.self) var viewModel var body: some View { // use @Bindable to get the binding @Bindable var viewModel = viewModel TextField("Text", text: $viewModel.text) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’23