SwiftUI:使用绑定初始化@StateObject

huangapple go评论68阅读模式
英文:

SwiftUI: Initialize @StateObject with binding

问题

在我的 init() 中,我试图通过传递到作用域中的另一个变量的绑定来初始化一个 @StateObject 变量。

我想实现类似这样的效果:

struct MyView: View {
    @State var value: Bool
    @StateObject var viewModel: ViewModel

    init() {
        self.value = false
        self.viewModel = ViewModel($value)
    }
}

我知道通常在初始化过程中需要属性时,我们可以简单地创建它们的副本并在初始化程序中使用副本。然而,由于我试图使用绑定,绑定到变量的副本是行不通的。

我该如何实现这一目标?谢谢!

英文:

In my init(), I'm trying to initialize a @StateObject variable by passing in a binding to another variable in scope.

I want to achieve something like this:

struct MyView: View {
    @State var value: Bool
    @StateObject var viewModel: ViewModel

    init() {
        self.value = false
        self.viewModel = ViewModel($value)
    }
}

I know that when properties are needed during initialization normally, we can just create a copy of them and use the copy in the initializer. However, since I'm trying to use a binding, binding to a copy of a variable won't work.

How can I achieve this? Thank you!

答案1

得分: 1

如果value已经与视图模型绑定,更好的做法是在视图模型中声明它为@Published属性。

class ViewModel : ObservableObject {
    @Published var value = false
}

struct MyView: View {
    @StateObject var viewModel = ViewModel()

    var body: some View {
        Toggle("切换某事", isOn: $viewModel.value)
    }
}
英文:

If value is bound to the view model anyway a better practice is to declare it in the view model as @Published property

class ViewModel : ObservableObject {
    @Published var value = false
}

struct MyView: View {
    @StateObject var viewModel = ViewModel()

    var body: some View {
        Toggle("Switch something", isOn: $viewModel.value)
    }
}

答案2

得分: 0

这会有帮助吗?使用此方法,您可以将属性值存储在 ViewModel 内部,然后稍后可以像 $viewModel.value 这样使用该值。

struct MyView: View {    
    @StateObject private var viewModel: ViewModel
    init() {
        _viewModel = StateObject(wrappedValue: ViewModel(value: false))
    }
}
英文:

Will this help?
With this, you can store the property value inside of the ViewModel and then you can use the value later with something like $viewModel.value.

struct MyView: View {    
    @StateObject private var viewModel: ViewModel
    init() {
        _viewModel = StateObject(wrappedValue: ViewModel(value: false))
    }
}

答案3

得分: 0

chuan322的回答是一个好回答,但你可以稍作修改如下:

结构体 MyView: View {
    @State var value: Bool
    @StateObject var viewModel: ViewModel

    init(value: Bool) {
        self.value = value
        _viewModel = StateObject(wrappedValue: ViewModel(value: value))
    }
}
英文:

chuan322's answer is a good answer but you can revise it a little as below:

struct MyView: View {
    @State var value: Bool
    @StateObject var viewModel: ViewModel

    init(value: Bool) {
        self.value = value
        _viewModel = StateObject(wrappedValue: ViewModel(value: value))
    }
}

huangapple
  • 本文由 发表于 2023年5月29日 14:22:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定