Variables 如何在Swiftui中更改不同文件中的变量值
我在contentviewVariables 如何在Swiftui中更改不同文件中的变量值,variables,swiftui,Variables,Swiftui,我在contentview@State var shouldshowmodel=false中设置了一个变量,我想在按下按钮shouldshowmodel=false后对其进行更改。我一直无法在范围中找到“shouldshowmodel”。下面是一个工作示例,通过@Bindings传递值。阅读有关@Binding的更多信息,或 这意味着您现在可以使用绑定执行shouldshowmodel=false,这也将更新包含@State的原始视图的主体 struct ContentView: View {
@State var shouldshowmodel=false
中设置了一个变量,我想在按下按钮shouldshowmodel=false
后对其进行更改。我一直无法在范围中找到“shouldshowmodel”。下面是一个工作示例,通过@Binding
s传递值。阅读有关@Binding
的更多信息,或
这意味着您现在可以使用绑定执行shouldshowmodel=false
,这也将更新包含@State
的原始视图的主体
struct ContentView: View {
@State private var shouldShowModal = false
var body: some View {
VStack {
Text("Hello world!")
.sheet(isPresented: $shouldShowModal) {
Text("Modal")
}
OtherView(shouldShowModal: $shouldShowModal)
}
}
}
struct OtherView: View {
@Binding var shouldShowModal: Bool
var body: some View {
VStack {
Text("Should show modal: \(shouldShowModal ? "yes" : "no")")
Toggle("Toggle modal", isOn: $shouldShowModal)
}
}
}
为了得到一个具体的建议,您必须显示比这更多的代码。一般来说,我已经阅读了
@Binding
以及如何在视图之间传递它: