简体   繁体   中英

Can i pass a Bool as an environment object to subViews in SwiftUI?

I have a bool

@State var isDragging: Bool

How can I pass this as an environment object to subViews?

You need to create an ObservableObject:

class Model: ObservableObject {
    @Published var isDragging: Bool = false
}

And then use:

struct MyView: View {
    @EnvironmentObject var mymodel: Model

    var body : some View {
        if mymodel.isDragging { ... }
    }
}

And also, you should watch to WWDC 2019 session "Data Flow in Swift". Although some of the type names have been changed since, the concepts remain the same.

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