import SwiftUI
struct StudyView: View {
@State var show = false // 动态改变的变量前面需要声明[@State]
var name = "字符串123" // 静态的变量不需要加[@State]
var body: some View {
VStack {
Toggle("切换", isOn: $show) // 控件与变量绑定需要加[$]
Text("这里是变量:\(name)").opacity(show ? 1 : 0)
Spacer() // 用于排版,自适应推开组件
}
.padding()
}
}
struct StudyView_Previews: PreviewProvider {
static var previews: some View {
StudyView()
}
}
标签:变量,show,语法,State,ui,var,StudyView,swift,View
From: https://www.cnblogs.com/NKnife/p/18113259