Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 如何在Swiftui中更改不同文件中的变量值_Variables_Swiftui - Fatal编程技术网

Variables 如何在Swiftui中更改不同文件中的变量值

Variables 如何在Swiftui中更改不同文件中的变量值,variables,swiftui,Variables,Swiftui,我在contentview@State var shouldshowmodel=false中设置了一个变量,我想在按下按钮shouldshowmodel=false后对其进行更改。我一直无法在范围中找到“shouldshowmodel”。下面是一个工作示例,通过@Bindings传递值。阅读有关@Binding的更多信息,或 这意味着您现在可以使用绑定执行shouldshowmodel=false,这也将更新包含@State的原始视图的主体 struct ContentView: View {

我在contentview
@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
以及如何在视图之间传递它: