How to set a custom environment key in SwiftUI?

前端 未结 1 954
青春惊慌失措
青春惊慌失措 2021-01-22 05:31

Creating a custom environment key works, but if I wish to set the value in a view, XCODE doesn\'t allow it. But the predefined environment values can be set. What am I doing wro

相关标签:
1条回答
  • 2021-01-22 06:23

    The Environment is used to pass values in parent > child direction, so value is set for usage. If you want to change internal of environment value then you need to wrap it somehow, possible variants are binding or reference type holder.

    Here is an example of usage based on binding (similar to how .editMode and .presentationMode work)

    struct TestResetEnv: View {
        @State private var isActive = false
        @State private var reset = false
        var body: some View {
            VStack {
                Text("Current: \(reset ? "true" : "false")")
                Button("Go") { self.isActive.toggle() }
                if isActive {
                    ResetView()
                }
            }.environment(\.resetDefault, $reset) // set for children as env!!
        }
    }
    
    struct ResetDefault: EnvironmentKey {
        static var defaultValue: Binding<Bool> = .constant(false)
    }
    
    extension EnvironmentValues {
        var resetDefault: Binding<Bool> {
            get { self[ResetDefault.self] }
            set { self[ResetDefault.self] = newValue }
        }
    }
    
    struct ResetView: View {
        @Environment(\.resetDefault) var reset
        var body: some View {
            Text("Reset").onAppear() {
                self.reset.wrappedValue.toggle() // << change wrapped !!
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题