如何预览包含与其父视图状态绑定的视图? [英] How do you preview a view containing a binding to its parent view's state?
问题描述
我将此视图作为其父视图的工作表呈现
I present this view as a sheet from its parent view
struct NamesView: View {
@Binding var match: Match
var body: some View {
...
}
}
由于 match
的真实来源在呈现此 NamesView
表的父视图中,因此在构建视图时,我传入了一个 $match
按预期绑定和数据流.
Since the match
source of truth is in the parent view presenting this NamesView
sheet, when the view is constructed I pass in a $match
binding and data flows as intended.
但是,在预览提供程序中构建此视图时
However, when constructing this view in a preview provider
struct NamesView_Previews: PreviewProvider {
static var previews: some View {
NamesView()
}
}
编译器说 NamesView()
需要一个 match
类型的参数 Binding
(Match
作为将此视图呈现为工作表的父视图).我不确定从这里开始的好方法是什么,或者这是否是 SwiftUI 的限制.
the compiler says that NamesView()
expects a match
argument of type Binding<Match>
(Match
being the parent view presenting this view as a sheet). I'm not sure what would be a good way to proceed from here or if this is a limitation of SwiftUI.
推荐答案
如果你只想不断预览,那么可以
If you want only constant preview, then it can be
struct NamesView_Previews: PreviewProvider {
static var previews: some View {
NamesView(match: .constant(Match()))
}
}
如果你想现场直播,它可以是
if you want it in live, the it can be
struct NamesView_Previews: PreviewProvider {
struct BindingTestHolder: View {
@State private var testedMatch = Match()
var body: some View {
NamesView(match: $testedMatch)
}
}
static var previews: some View {
BindingTestHolder()
}
}
这篇关于如何预览包含与其父视图状态绑定的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!