我正在从子组件“ LessonView”的父视图“ LessonOverview”中更新状态“ lessonVisible”
如果代码按预期工作并且其状态在父级中更新,则代码本身即为自身。但是该代码不适用于预览。
我在预览提供程序结构中的第45行出现以下错误:
无法将类型“ visibleLessonStruct”的值转换为预期值 参数类型“绑定”
在线:
LessonView(lessonVisible:visibleLessonStruct(lessonType: .Reading))。previewDevice(PreviewDevice(rawValue:“ iPhone XS”))
如何解决此问题?
import Foundation
import SwiftUI
struct visibleLessonStruct{
var lessonType: LessonType
}
enum LessonType{
case Reading, Listening, Practice
}
struct LessonView: View {
@Binding var lessonVisible : visibleLessonStruct!
var body: some View {
GeometryReader { geometry in
VStack{
HStack{
Spacer()
Button(action: {
print("Close button tapped!")
self.lessonVisible = nil
}) {
Image("cross").renderingMode(.original).resizable().frame(width: 40, height: 40)
}
}.padding(10).padding(.trailing, 10)
Spacer()
}.background(Color.white).statusBar(hidden: true)
}
}
}
struct LessonView_Previews: PreviewProvider {
static var previews: some View {
LessonView(lessonVisible: visibleLessonStruct(lessonType: .Reading)).previewDevice(PreviewDevice(rawValue: "iPhone XS"))
}
}
答案 0 :(得分:0)
尝试将其包装在.constant
内,如下所示:
struct LessonView_Previews: PreviewProvider {
static var previews: some View {
LessonView(lessonVisible: .constant(visibleLessonStruct(lessonType: .Reading))).previewDevice(PreviewDevice(rawValue: "iPhone XS"))
}
}
常量在预览中用于模仿Bindable和State变量的行为。