如何解决SwiftUI中多个在2 TextField上发布的死锁? [英] How to solve deadlock on multiple Published on 2 TextField in SwiftUI?

查看:43
本文介绍了如何解决SwiftUI中多个在2 TextField上发布的死锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图模型中有两个发布状态,一个用于 source ,另一个用于 destination

基于解决方案

  class SourceDestinationViewModel:ObservableObject {let rate:Double = 2//仅用于演示@Published var source:Double = 0 {didSet {让newValue =源/费率如果目的地!= newValue {目的地= newValue}}}@已发布的var目的地:Double = 0 {didSet {让newValue =目标*费率如果source!= newValue {来源= newValue}}}}struct TestDependentFields:查看{@ObservedObject var vm = SourceDestinationViewModel()var body:some View {VStack {TextField(",文本:Binding< String>(get:{String(self.vm.source)},设置:{self.vm.source =($ 0作为NSString).doubleValue}))TextField(",文本:Binding< String>(get:{String(self.vm.destination)},设置:{self.vm.destination =($ 0作为NSString).doubleValue}))垫片()}}} 

I have two Published state in my viewmodel one is for source and another one is for destination

Based on the solution here to detect live changes on textfield. I have implemented like this

TextField("", text: $viewModel.source)
TextField("", text: $viewModel.destination)

Problem, here is i need to update the either fields whenever user enter some value. For example - If user enter on source then i need to update destination with source inputs. And if user input destination then need to update source.

@Published var source: Double = 0 {
        didSet {
            destination = source / rate
        }
    }

This is just one solution. I have to repeat it same for destination then it will create deadlock

@Published var destination: Double = 0 {
        didSet {
            source = destination * rate
        }
    }

How to solve this problem ? Only solution i imagine if there is any way i can get live changes in the form of callback then i can update the either field but i don't know if it is possible

解决方案

Here is possible solution. Tested with Xcode 11.4 / iOS 13.4

class SourceDestinationViewModel: ObservableObject {
    let rate: Double = 2 // just for demo
    
    @Published var source: Double = 0 {
        didSet {
            let newValue = source / rate
            if destination != newValue {
                destination = newValue
            }
        }
    }

    @Published var destination: Double = 0 {
        didSet {
            let newValue = destination * rate
            if source != newValue {
                source = newValue
            }
        }
    }
}

struct TestDependentFields: View {
    @ObservedObject var vm = SourceDestinationViewModel()

    var body: some View {
        VStack {
            TextField("", text: Binding<String>(get: { String(self.vm.source) },
                set: { self.vm.source = ($0 as NSString).doubleValue }))
            TextField("", text: Binding<String>(get: { String(self.vm.destination) },
                set: { self.vm.destination = ($0 as NSString).doubleValue }))

            Spacer()
        }
    }
}

这篇关于如何解决SwiftUI中多个在2 TextField上发布的死锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆