ios - 我需要为我的预览调用添加一个参数。我应该添加什么而不说明布尔值是真还是假?
问题描述
我需要为我的预览调用添加一个参数。我应该添加什么而不说明布尔值是真还是假?
import SwiftUI
struct LiberteIDCreation: View {
@Binding var LoginSucess: Bool
var body: some View {
VStack {
return Group {
if LoginSucess {
ContentView()
} else {
LiberteIDLogin(LoginSucess: self.LoginSucess)
}
}
}
}
}
struct LiberteIDCreation_Previews: PreviewProvider {
static var previews: some View {
LiberteIDCreation(LoginSucess: Bool) //what should I add for this? I need to add an agrument for this
}
}
我可以向 LiberteLDCreation 添加什么以使其正常工作?它说它需要一个论点?
解决方案
答案是:你应该使用 .constant(false)
in LiberteIDCreation_Previews
。但这是您需要知道的另一个问题。在您的情况下(使用语句),它在画布中无法正常工作if...else
,视图不会更新。至少在我的 Xcode 版本中它不起作用。试试下面的例子来检查。:
struct UsingBindingBool: View {
@Binding var loginSuccess: Bool
var body: some View {
VStack {
return Group {
if loginSuccess {
Text("success")
} else {
Text("change variable from child").onTapGesture { self.loginSuccess = true }
}
Text("other group elements...")
}
}
}
}
struct UsingStateForBinding: View {
@State private var loginSuccess = false
var body: some View {
VStack {
UsingBindingBool(loginSuccess: $loginSuccess)
.padding(.bottom)
Text("presenting parent button:").foregroundColor(.red)
Button(action: {self.loginSuccess = true}) { Text("change to true from parent") }
}
}
}
struct UsingBindingBool_Previews: PreviewProvider {
static var previews: some View {
Group {
UsingBindingBool(loginSuccess: .constant(false))
UsingStateForBinding()
}
}
}
推荐阅读
- python - 基于 Python 中的属性值从 XML 节点中删除节点
- javascript - 是否可以使用 Restana 作为 Node.js 中服务器的框架来发送文件作为响应?
- javascript - js onscroll child div 相对固定到它的parentNode
- mysql - Doctrine 是否可以在表外使用鉴别器进行继承映射
- python - Kinesis 中用于 python 消费者和生产者的模式演变
- python - Matlotlib:如何在循环中优化性能重用艺术家
- c - x86-64 GCC 生成将寄存器移动到自身的代码
- visual-studio-code - VS Code - 如何使向下箭头向下移动而不是导航建议
- assembly - 大会:方括号中的寄存器是什么意思?
- c++ - 带有 ref-qualifier 的 getter 的最佳实践