因此,我在视图中声明了一个绑定,在生成预览时,我似乎无法让它工作,而且它一直在崩溃。
因此,我有以下看法:
struct MapPinSheetSectionOneView: View {
// Safari View - Binding
@Binding var isBrowsingWebsite: Bool
// Variables
let item: Place
// Main body
var body: some View {
HStack(alignment: .top, spacing: 0) {
// Type/Title/Excerpt
showSectionOne
.border(.blue)
// Spacers
Spacer()
Spacer()
Spacer()
// Link & Close Button
showSheetLinkAndCloseButton
.border(.red)
}
.border(.red)
// Divider
Divider()
}
// MARK: Category, Title & Excerpt
private var showSectionOne: some View {
VStack(alignment: .leading, spacing: 0) {
Group {
if item.category != "" {
Text(verbatim: item.category)
.padding(8)
.background(
RoundedRectangle(cornerRadius: 8)
.fill(Color.accentColor)
)
}
if item.title != "" {
Text(item.title)
.font(.title2.weight(.semibold))
}
if item.excerpt != "" {
HStack(alignment: .top, spacing: 3) {
Text(item.excerpt)
.font(.footnote)
.foregroundColor(.secondary)
.lineLimit(2)
Spacer()
}
}
}
.lineLimit(1)
}
}
// MARK: Link & Close Button
private var showSheetLinkAndCloseButton: some View {
Group {
if item.website != "" {
Button(action: {
self.isBrowsingWebsite = true
}) {
Image(systemName: "link.circle.fill")
}
.sheet(isPresented: $isBrowsingWebsite) {
SafariViewWrapper(url: URL(string: item.website)!)
}
}
Image(systemName: "xmark.circle.fill")
}
.imageScale(.large)
.foregroundColor(.accentColor)
}
}然后,我尝试预览以下内容:
struct MapPinSheetSectionOneView_Previews: PreviewProvider {
@Binding var isBrowsingWebsite: Bool
static var previews: some View {
MapPinSheetSectionOneView(
isBrowsingWebsite: $isBrowsingWebsite,
item: Place(
id: 0,
title: "Title",
category: "Category",
type: "Type",
description: "Description",
excerpt: "Excerpt",
address: "Address",
city: "City",
state: "State",
zipcode: 0,
country: "Country",
lat: 39.828194,
long: -98.569611,
altitude: 0,
amenity: ("Amenities"),
admission: "Free",
website: "Website"
)
)
}
}由于某种原因,它不断崩溃,我得到了以下错误:
实例成员'$isBrowsingWebsite‘不能用于'MapPinSheetSectionOneView_Previews’类型
有谁知道如何在预览中使绑定bool工作吗?
发布于 2022-11-19 20:41:21
方法1-动态值
为了在预览中使用绑定(而不声明为常量),您需要创建一个包装器。
我必须为Place创建一个结构,因为它是不包括的。你可以用你的版本的Place交换它
struct PreviewWrapperWithState<Value, Content: View>: View {
@State var value: Value
var content: (Binding<Value>) -> Content
var body: some View {
content($value)
}
init(_ value: Value, content: @escaping (Binding<Value>) -> Content) {
self._value = State(wrappedValue: value)
self.content = content
}
}
struct MapPinSheetSectionOneView_Previews: PreviewProvider {
static var previews: some View {
var thingToPreview = false // ERROR
let place = Place(
id: 0,
title: "Title",
category: "Category",
type: "Type",
description: "Description",
excerpt: "Excerpt",
address: "Address",
city: "City",
state: "State",
zipcode: 0,
country: "Country",
lat: 39.828194,
long: -98.569611,
altitude: 0,
amenity: ("Amenities"),
admission: "Free",
website: "Website"
)
PreviewWrapperWithState(thingToPreview) { MapPinSheetSectionOneView(isBrowsingWebsite: $0, item: place) }
}
}方法2-常数值
另一种方法是使用常量初始化绑定。这将将功能限制在其初始化状态。
struct MapPinSheetSectionOneView_Previews: PreviewProvider {
static var previews: some View {
MapPinSheetSectionOneView(
isBrowsingWebsite: Binding.constant(true),
item: Place(
id: 0,
title: "Title",
category: "Category",
type: "Type",
description: "Description",
excerpt: "Excerpt",
address: "Address",
city: "City",
state: "State",
zipcode: 0,
country: "Country",
lat: 39.828194,
long: -98.569611,
altitude: 0,
amenity: ("Amenities"),
admission: "Free",
website: "Website"
)
)
}
}https://stackoverflow.com/questions/74502632
复制相似问题