swiftui - Picker selection not updating the view - Stack Overflow

In my app there is a view with several pickers. If I select a value with one of them ($limits[index].fr

In my app there is a view with several pickers. If I select a value with one of them ($limits[index].from), a value that depends on it should be updated. Unfortunately, this does not work. Only when I select the desired value a second time is the dependent value (limits[index + 1].to) also displayed correctly.

Here's my code:

import SwiftUI

class Limit: Identifiable {
   var value: Int
   var from: Double
   var to: Double

   init(value: Int, from: Double, to: Double) {
      self.value = value
      self.from = from
      self.to = to
   }
}

struct ContentView: View {

   @State private var limits: [Limit] = [
      Limit(value: 1, from: 10.5, to: 12),
      Limit(value: 2, from: 8.5, to: 10),
      Limit(value: 3, from: 6.5, to: 8),
      Limit(value: 4, from: 4.5, to: 6),
      Limit(value: 5, from: 2.5, to: 4),
      Limit(value: 6, from: 0, to: 2)
   ]
   private var sum:Double = 12

   var body: some View {
      VStack {
         ForEach(0..<limits.count, id: \.self) { index in
            HStack {
               Text("Value \(limits[index].value): ")
               Text("\(limits[index].to, specifier: "%.1f")")
               Text("   - ")
                
               let values = getChangeValues(limit: limits[index])
                
               Picker("from", selection: $limits[index].from) {
                  ForEach(values, id: \.self) { value in
                     Text("\(value, specifier: "%.1f")").tag(value)
                  }
               }
               .onChange(of: limits[index].from) {
                  limits[index + 1].to = limits[index].from - 0.5
               }
            }
         }
      }
      .padding()
   }

   private func getChangeValues(limit: Limit) -> [Double] {
      return Array(stride(from: limit.from - 0.5 * floor(sum / 6), through: limit.from + 0.5 * floor(sum / 6), by: 0.5))
   }
}

I don't know how to solve it.

In my app there is a view with several pickers. If I select a value with one of them ($limits[index].from), a value that depends on it should be updated. Unfortunately, this does not work. Only when I select the desired value a second time is the dependent value (limits[index + 1].to) also displayed correctly.

Here's my code:

import SwiftUI

class Limit: Identifiable {
   var value: Int
   var from: Double
   var to: Double

   init(value: Int, from: Double, to: Double) {
      self.value = value
      self.from = from
      self.to = to
   }
}

struct ContentView: View {

   @State private var limits: [Limit] = [
      Limit(value: 1, from: 10.5, to: 12),
      Limit(value: 2, from: 8.5, to: 10),
      Limit(value: 3, from: 6.5, to: 8),
      Limit(value: 4, from: 4.5, to: 6),
      Limit(value: 5, from: 2.5, to: 4),
      Limit(value: 6, from: 0, to: 2)
   ]
   private var sum:Double = 12

   var body: some View {
      VStack {
         ForEach(0..<limits.count, id: \.self) { index in
            HStack {
               Text("Value \(limits[index].value): ")
               Text("\(limits[index].to, specifier: "%.1f")")
               Text("   - ")
                
               let values = getChangeValues(limit: limits[index])
                
               Picker("from", selection: $limits[index].from) {
                  ForEach(values, id: \.self) { value in
                     Text("\(value, specifier: "%.1f")").tag(value)
                  }
               }
               .onChange(of: limits[index].from) {
                  limits[index + 1].to = limits[index].from - 0.5
               }
            }
         }
      }
      .padding()
   }

   private func getChangeValues(limit: Limit) -> [Double] {
      return Array(stride(from: limit.from - 0.5 * floor(sum / 6), through: limit.from + 0.5 * floor(sum / 6), by: 0.5))
   }
}

I don't know how to solve it.

Share Improve this question edited Mar 25 at 16:34 user2836375 asked Mar 25 at 14:54 user2836375user2836375 1392 silver badges11 bronze badges 3
  • How is this different from your previous question? – Joakim Danielson Commented Mar 25 at 17:14
  • I have outsourced the problem to a separate project. This is now the entire code so that the problem is also reproducible. – user2836375 Commented Mar 25 at 18:49
  • Change class to struct and use Binding. Watch “Demystify SwiftUI” – lorem ipsum Commented Mar 25 at 22:01
Add a comment  | 

1 Answer 1

Reset to default 0

The secret was to replace class with struct. Then everything worked as desired. Thanks to lorem ipsum!

Here is the (slightly) adjusted code:

struct Limit {
    var value: Int
    var from: Double
    var to: Double
    
    init(value: Int, from: Double, to: Double) {
        self.value = value
        self.from = from
        self.to = to
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744187796a4562289.html

相关推荐

  • swiftui - Picker selection not updating the view - Stack Overflow

    In my app there is a view with several pickers. If I select a value with one of them ($limits[index].fr

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信