简体   繁体   English

如何从 swiftUI 的父视图中的另一个视图访问变量?

[英]How to access variable from another view in parent view in swiftUI?

I have one view "CancelRow"我有一个视图“CancelRow”

struct CancelRow: View {
    @State var isSelected: Bool = true // its dynamic

//UI part
}

I have another view i which i want to show button active or not based on "isSelected" from "CancelRow"我有另一个视图,我想根据“CancelRow”中的“isSelected”显示按钮是否处于活动状态

// AnotherVIew // 另一个视图

    Spacer()
    SubmitButtonView(buttonTitle: title, buttonCallBack: {
        goToOtherScreen()
    }, isActive: isSelected ) // how to access this variable from  "CancelRow"

but i am not sure how to access variable from another view..and if value of "isSelected" is changed in "CancelRow" how to update that value in another view?但我不确定如何从另一个视图访问变量。如果在“CancelRow”中更改了“isSelected”的值,如何在另一个视图中更新该值?

Thank you for help谢谢你的帮助

If the relationship beetween your views is "parent" (or composition ), then you can use @Binding property wrapper.如果您的视图之间的关系是“父”(或组合),那么您可以使用@Binding属性包装器。 Then your SubmitButtonView will have a property然后你的SubmitButtonView将有一个属性

@Binding var isActive: Bool 

and you'll call你会打电话给

SubmitButtonView(buttonTitle: title, buttonCallBack: {
        goToOtherScreen()
    }, isActive: $isSelected )

from CancelRow .CancelRow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM