因此,我在 View 中声明了一个 Binding,但在生成预览时似乎无法使其工作,并且它不断崩溃。
所以我有以下观点:
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"
)
)
}
}
由于某种原因,它不断崩溃,并且我收到以下错误:
Instance member '$isBrowsingWebsite' cannot be used on type 'MapPinSheetSectionOneView_Previews'
有人知道如何使绑定(bind) bool 在预览中工作吗?
最佳答案
方法 1 - 动态值(value)
为了在预览中使用绑定(bind)(无需将其声明为常量),您需要创建一个包装器。
我必须为 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 - 恒定值
另一种方法是使用常量初始化您的绑定(bind)。这会将功能限制为其初始化状态。
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"
)
)
}
}
关于swift - 实例成员 '$isBrowsingWebsite' 不能用于类型 'MapPinSheetSectionOneView_Previews',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74502632/