SwiftUI MVVM绑定

SwiftUI MVVM绑定,swiftui,Swiftui,如何将@Binding传递到ViewModel 让我们考虑一个简单的例子, struct-TopView:View{ @表示状态变量:Bool var body:一些观点{ 子视图(isPresented:$isPresented) } } 结构子视图:视图{ @显示绑定变量:Bool } 然后,如果我们想要一个MVVM模式 在可观察对象中使用@Binding安全吗 比如说,写东西安全吗 struct-TopView:View{ @表示状态变量:Bool var body:一些观点{ 子视图(

如何将
@Binding
传递到ViewModel

让我们考虑一个简单的例子,

struct-TopView:View{
@表示状态变量:Bool
var body:一些观点{
子视图(isPresented:$isPresented)
}
}
结构子视图:视图{
@显示绑定变量:Bool
}
然后,如果我们想要一个MVVM模式

可观察对象中使用
@Binding
安全吗

比如说,写东西安全吗

struct-TopView:View{
@表示状态变量:Bool
var body:一些观点{
子视图(模型:子视图模型(显示:$isPresented))
}
}
结构子视图:视图{
@观察对象var模型:子视图模型
}
//在“SubViewModel.swift”中
进口基金会
进口联合收割机
导入快捷键
公共最终类子视图模型:ObservedObject{
@显示绑定变量:Bool
public init(isPresented:Binding){
self.\u isPresented=isPresented
}
}

你会怎么做?类似,包括如何将environmentObject传递到视图模型?

您描述的是
可观察对象的基本用法。
@Binding
@State
仅在
视图中使用

Apple SwiftUI教程可能会非常有帮助,让您了解SwiftUI的概念

您的代码中有一些基本错误,我在下面的代码中提到了这些更改

import SwiftUI

struct PassingBinding: View {
    var body: some View {
        TopView1()//Best Practice
        //TopView2()//Singleton Pattern
    }
}
///This is the best way to do it
///https://developer.apple.com/tutorials/swiftui/handling-user-input
struct TopView1: View {
    @ObservedObject var model: SubViewModel = SubViewModel()
    var body: some View {
        VStack{
            SubView1().environmentObject(model)
            
            Button(action: {
                self.model.isPresented.toggle()
            }, label: {
                Text("Toggle isPresented")
            })
        }
    }
}

struct SubView1: View {
    @EnvironmentObject var model: SubViewModel
    var body: some View {
        Text("SubView - isPresented == \(model.isPresented.description)")
    }
}
///Another way that has some specifc uses is to use a Singleton model
///https://developer.apple.com/documentation/swift/cocoa_design_patterns/managing_a_shared_resource_using_a_singleton
struct TopView2: View {
    @ObservedObject var model: SubViewModel = SubViewModel.shared
    var body: some View {
        VStack{
            SubView2()
            
            Button(action: {
                self.model.isPresented.toggle()
            }, label: {
                Text("Toggle isPresented")
            })
        }        }
}

struct SubView2: View {
    @ObservedObject var model: SubViewModel = SubViewModel.shared
    var body: some View {
        Text("SubView - isPresented ==  \(model.isPresented.description)")
    }
}

///This item can be Observed "ObservableObject" vs I am Observing this object "ObservedObject"
///https://developer.apple.com/documentation/combine/observableobject
public final class SubViewModel: ObservableObject {
    static let shared: SubViewModel = SubViewModel()//Singleton Pattern 
    
    @Published var isPresented: Bool = false //Initialize here this is the source for the View
    //@Binding and @State is are only used in View struct not in an ObservableObject.https://developer.apple.com/documentation/swiftui/binding
    
    
}
struct PassingBinding_Previews: PreviewProvider {
    static var previews: some View {
        PassingBinding()
    }
}