Preview 中传递参数
当在页面间传递参数的时候会使用到 @Binding 声明,但是此时在 PreViews 中就会报错提示需要传递相应的参数进行初始化才能预览。
解决办法就是先声明 @State static 的静态参数,然后传递进去,举个例子:
import SwiftUI
struct CategorySelectView: View {
@Binding var categories: [String]
@Binding var categorySelectedIndex: Int
var body: some View {
List {
ForEach(categories, id: \.self) { category in
Text(category)
}
}
}
}
struct CategorySelectView_Previews: PreviewProvider {
@State static var categories: [String] = ["上衣", "裤子", "其他"]
@State static var categorySelectedIndex: Int = 0
static var previews: some View {
CategorySelectView(categories: $categories, categorySelectedIndex: $categorySelectedIndex)
}
}