简体   繁体   中英

How to access variable from another view in parent view in swiftUI?

I have one view "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"

// 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?

Thank you for help

If the relationship beetween your views is "parent" (or composition ), then you can use @Binding property wrapper. Then your SubmitButtonView will have a property

@Binding var isActive: Bool 

and you'll call

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

from CancelRow .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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