【问题标题】:Passing steper value to another View - SwiftUI将步进值传递给另一个视图 - SwiftUI
【发布时间】:2021-06-12 02:37:08
【问题描述】:

我在 SwiftUI 中有一个非常简单的“应用程序”

如何将步进值从列表传递到 struct SumOfValue 或 ContentView ?但我想传递步进值的总和,以防图像中的值为 8。

struct ContentView: View {
    var body: some View {
        VStack{
            List{
                ProductList()
                ProductList()
                
            }
            Spacer()
            Text("Sum of stepper value: ?????")
                .padding(.bottom, 50
                )
            
            SumOfValue()
        }
    }
}

struct ProductList:View {
    @State var stepperValueTest: Int = 0
    var body: some View {
        HStack {
            Stepper("Value: \(stepperValueTest)", value: $stepperValueTest)
        }   
    }
}

struct SumOfValue: View {
    var body: some View {
        Text("or here sum of value: ????? ")
            .foregroundColor(Color.red)
    }
}

我尝试使用@Binding,但没用。

【问题讨论】:

    标签: swiftui stepper


    【解决方案1】:

    这里有多种方法,归根结底是数据组织的问题。

    一种思考方式是,有一个父值数组——在你的例子中是ContentView——“拥有”,并且每个孩子使用绑定更新他们在该数组中分配的值。这样,由于父级拥有整个数组,因此可以轻松计算这些值的总和。

    struct ContentView: View {
       @State var values = [0,0,0]
    
       var body: some View {
          VStack {
             List {
                ProductList(stepperValueTest: $values[0])
                ProductList(stepperValueTest: $values[1])
                ProductList(stepperValueTest: $values[2])
             }
             Text("Sum: \(sum)")
          }
       }
    
       var sum: Int { values.reduce(0, +) }
    }
    
    struct ProductList:View {
        @Binding var stepperValueTest: Int // change to Binding
        var body: some View {
            HStack {
                Stepper("Value: \(stepperValueTest)", value: $stepperValueTest)
            }   
        }
    }
    

    【讨论】:

      【解决方案2】:

      @State 进入父级(ContentView),@Binding 进入子级(ProductList 和 SumOfValue)。

      试试这个:

      struct ContentView: View {
      
          /// States here!
          @State var firstStepperValue: Int = 0
          @State var secondStepperValue: Int = 0
      
          var body: some View {
              VStack{
                  List{
      
                      /// pass it in here!
                      ProductList(stepperValueTest: $firstStepperValue)
                      ProductList(stepperValueTest: $secondStepperValue)
                      
                  }
                  Spacer()
      
                  /// add the values here
                  Text("Sum of stepper value: \(firstStepperValue + secondStepperValue)")
                      .padding(.bottom, 50
                      )
                  
                  /// you can also pass it in here
                  SumOfValue(first: $firstStepperValue, second: $secondStepperValue)
                      .padding(.bottom, 100)
              }
          }
      }
      
      struct ProductList:View {
      
          /// Binding here!
          @Binding var stepperValueTest: Int
          var body: some View {
              HStack {
                  Stepper("Value: \(stepperValueTest)", value: $stepperValueTest)
              }
          }
      }
      
      struct SumOfValue: View {
          @Binding var first: Int
          @Binding var second: Int
          
          var body: some View {
              Text("or here sum of value: \(first + second) ")
                  .foregroundColor(Color.red)
          }
      }
      

      结果:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        • 2022-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-18
        • 2022-06-10
        相关资源
        最近更新 更多