1
votes

I wonder how I can pass down the binding variable to Slider view

struct TodayItemView: View {

   @State var progress = 0.0

   var body: some View {
       VStack {
           Slider(value: $progress,
                   in: 0...1,
                   step: 0.25)
       }
   }
}

If I change the @State to @Binding, it shows a lot of error messages like below

Cannot convert value of type 'Double' to specified type 'Binding'

Extraneous argument label 'wrappedValue:' in call

Generic parameter 'V' could not be inferred

But, I want to make it have @Binding for that variable because I am going to use this view in another view.

Is there any solution for this?

1

1 Answers

2
votes

Your view that has the @Binding can't own/store the value. The compiler error is coming from = 0.0

What you need to do is create a wrapper view that stores the value (for when you want to use it without passing in a value)

struct TodayItemView: View {

   @Binding var progress: Double

   var body: some View {
       VStack {
           Slider(value: $progress,
                   in: 0...1,
                   step: 0.25)
       }
   }
}

struct SelfContainedTodayItemView {
    @State var progress: Double = 0.0
    
    var body: some View {
        TodayItemView(progress: $progress)
    }
}

Then you can use SelfContainedTodayItemView in cases were you do not pass in the binding and use TodayItemView for cases were you do.