使用视图模型与在视图本身中使用 @Binding 时如何使用绑定? [英] How to work with bindings when using a view model vs using @Binding in the view itself?

查看:23
本文介绍了使用视图模型与在视图本身中使用 @Binding 时如何使用绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@State@BindingSwiftUI 中工作得很好,只要你把所有视图的数据都放在里面,就像这样:

@State and @Binding work so well in SwiftUI, as long as you put all the view's data inside itself, like this:

struct ColorView: View {
    @Binding public var isBlue: Bool

    var body: some View {
        Rectangle()
            .foregroundColor(isBlue ? .blue : .red)
            .onTapGesture {
                self.isBlue.toggle()
        }
    }
}

struct TestView: View {
    @State var isBlue: Bool = false

    var body: some View {
        ColorView(isBlue: $isBlue)
    }
}

它可以正常工作,而且非常简单.但是 MVVM 说你应该把所有视图的数据放在一个视图模型类中,以将 UI 与模型分开.但是这样你就完全失去了 @State@Binding .您似乎失去了这种 2 向绑定.当然,您可以使用 Combine 或其他方法手动完成,但这不应该是正确的方法,对吗?

It works without a problem and it's really simple. But MVVM says you should put all of the view's data inside a view model class, to separate UI from the model. But then you lose @State and @Binding completely. You lose this 2-way binding it seems. Sure, you can do it manually with Combine or something but that should not be the correct way, right?

每当我尝试任何事情时,当您使用视图模型时,SwiftUI 真的非常简单和方便.然而,一旦你把所有东西都放在一个视图模型类中,一切都会崩溃,没有什么比这更方便的了.这不可能,他们早就想到了.所以我在这里遗漏了一些东西.我真的很感激任何帮助.您将如何使用视图模型对上述示例进行编码(无需手动破解"任何内容)?我试过了,但它甚至没有编译:

Whenever I try anything, SwiftUI is really easy and convenient when you don't use view models. Once you put everything inside a view model class, though, everything breaks down and nothing works as convenient anymore. This can't be the case, they have to had thought of that. So I'm missing something here. I'd really appreciate any help. How would you code the above's example using view models (without "hacking" anything manually)? I tried but it doesn't even compile:

struct ColorView: View {
    @ObservedObject var viewModel: ViewModel

    class ViewModel: ObservableObject {
        // Binding or Published? Both doesn't seem to work
        @Binding var isBlue: Bool

        init(isBlue: Binding<Bool>) { // Or type Bool? But then we lose the binding
            self.$isBlue = isBlue
        }
    }

    var body: some View {
        Rectangle()
            .foregroundColor(viewModel.isBlue ? .blue : .red)
            .onTapGesture {
                self.viewModel.isBlue.toggle()
        }
    }
}

struct TestView: View {
    @ObservedObject var viewModel: ViewModel

    class ViewModel: ObservableObject {
        @Published var isBlue: Bool = false // We would need a @State here, but then we lose @Published
    }

    var body: some View {
        ColorView(viewModel: .init(isBlue: /* ??? */)) // How to pass a binding here`
    }
}

我认为这是错误的方式吗?

Do I think of this the wrong way?

谢谢!

推荐答案

使用MVVM是你的选择,苹果官方不推荐使用任何设计模式.他们为我们提供了以下概念,这实际上取决于我们如何使用它们.

Using MVVM is your choice, apple officially does not recommend to use any design pattern. They have given us below concepts and it truly depends on us how we can use them.

1- 属性包装器(@State、@Binding、@Environment)

1- Property wrappers(@State, @Binding, @Environment)

2- SwiftUI 替换情节提要

2- SwiftUI replacement of Storyboards

3- Combine 框架提供了一个声明性的 Swift API,用于随着时间的推移处理值,如 RxSwift、RxJava、RxKotlin 等.

3- Combine framework provides a declarative Swift API for processing values over time like RxSwift, RxJava, RxKotlin etc.

如果您想使用 MVVM,您仍将使用上述所有框架

If you want to use MVVM you will still be using all of the above frameworks

视图:使用 SwiftUI 设计和绑定视图.

View: Design and bind the view with SwiftUI.

ViewModel:在这里您将使用@Observable、@Published 属性并使用 SwiftUI 绑定概念将其与 View 绑定.

ViewModel: Here you will use @Observable, @Published properties and bind it with View using SwiftUI binding concepts.

模型:这里我们使用简单的数据模型.

Model: Here we are using simple data models.

网络:这里我们将使用组合框架发布者/订阅者机制来处理网络流.

Network: Here we will be using Combine framework publisher/subscriber mechanism for handling network streams.

注意:您可以将上述所有概念结合起来,并在另一个MVP、MVC 或 VIPER 等设计模式.

NOTE: You can combine all the above concepts and use it in another design pattern like MVP, MVC or VIPER.

这篇关于使用视图模型与在视图本身中使用 @Binding 时如何使用绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆