【问题标题】:SwiftUI ViewModel updateSwiftUI ViewModel 更新
【发布时间】:2019-12-11 17:19:37
【问题描述】:

我有一个带有许多按钮的视图,如果用户点击一个按钮,视图模型需要用增加的值更新当前按钮。

class ProductVM: ObservableObject {
    @Published var product : Product


    init(product: Product) {
        self.product = product
    }


    public func increaseAmount() {
        var myInt = Int(self.product.amount) ?? 0
        myInt += 1
        self.product.amount = String(myInt)

        print(myInt)
        print("...")
    }
}

问题是 myInt 每次都只有 1 并且值无法更新。

如何更新值并将其保存在当前模型中,以便视图知道它的增加??!!

struct singleButtonView: View {
@ObservedObject var productVM : ProductVM




func updatePos(){
    self.productVM.increaseAmount()
  }
 }

我用它来称呼它

singleButtonView(productVM: ProductVM(product: product))

【问题讨论】:

  • myInt的原因是什么?直接更新product.amount。做到这一点 - 正确 - 一切都会顺利进行。
  • 它是一样的。我将值更改为 Integer 并直接更新,没有任何反应。
  • 其他代码可以吗?或者您有什么问题可以看到?

标签: swiftui


【解决方案1】:

嵌套ObservableObjects 需要手动更新。这是一个示例:

class Product: ObservableObject, Identifiable, Codable {
    let id: Int
    let name: String
    let prize: Double
    @Published var amount: Int = 0

    enum CodingKeys: String, CodingKey {
        case id
        case name
        case prize
        case amount
    }

    init() {
        self.id = 0
        self.name = "name"
        self.prize = 0
    }

    init(id: Int, name: String, prize: Double) {
        self.id = id
        self.name = name
        self.prize = prize
    }

    required init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)

        id = try values.decode(Int.self, forKey: .id)
        name = try values.decode(String.self, forKey: .name)
        prize = try values.decode(Double.self, forKey: .prize)
        amount = try values.decode(Int.self, forKey: .amount)
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(name, forKey: .name)
        try container.encode(prize, forKey: .prize)
        try container.encode(amount, forKey: .amount)
    }
}


class ProductVM: ObservableObject {
    @Published var product: Product
    var cancelable: AnyCancellable? = nil

    init(product: Product) {
        self.product = product
        self.cancelable = product.objectWillChange.sink(receiveValue: {
            self.objectWillChange.send()
        })
    }

    public func increaseAmount() {
        self.product.amount += 1
    }
}


struct ContentView: View {
    @ObservedObject var productVM = ProductVM(product: Product())

    var body: some View {
        VStack {
            Button(action: {
                self.productVM.increaseAmount()
            }) {
                Text("Add")
            }
            Text("\(self.productVM.product.amount)")
        }
    }
}

我希望这会有所帮助! Credits

【讨论】:

  • 如何用我的产品结构的值填充产品类?
  • 哪些值?您可以将 @Published 变量绑定到 UI,例如 TextField
  • 就像我向您展示的那样...将您的产品严格更改为一个类,从 ObservableObject 继承并添加 @Published 属性包装器...然后添加我在 ProductVM 中的内容
  • @xyzcodeeee 我在Product 类中添加了您要求的内容
  • 我还有其他问题。但是这个问题现在已经解决了。
猜你喜欢
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
相关资源
最近更新 更多