简体   繁体   English

SwiftUI 和组合 - 当另一个更改时更新视图模型中的变量

[英]SwiftUI and Combine - Update variable in view model when another changes

I am currently building a small one view app to help learn some more SwiftUI.我目前正在构建一个小的单一视图应用程序来帮助学习更多的 SwiftUI。 The app is a simple app to calculate values in a ratio.该应用程序是一个简单的应用程序,用于计算比率值。 I have 4 main variables at the moment, the left and right side of the ratio, and the first and second value that will be calculated in ratio.我目前有 4 个主要变量,比率的左侧和右侧,以及将以比率计算的第一个和第二个值。 Initially I had each of these stored as @State properties:最初,我将其中的每一个都存储为@State属性:

@State var leftSide = Double()
@State var rightSide = Double()
@State var value1 = Double()
@State var value2 = Double()

In the app there are 4 sliders that allow each variable to be changed independently of each other.在应用程序中有 4 个滑块,允许每个变量相互独立地更改。 What I want to now allow to happen is that changing the variable value1 will also update value2 according to the ratio the user has set.我现在希望允许发生的是,更改变量value1也将根据用户设置的比率更新value2 To achieve this I believe I need to use Combine however I have not got much experience with the framework.为了实现这一点,我相信我需要使用Combine,但是我对该框架没有太多经验。

To begin with I moved the variables to an ObservableObject class:首先,我将变量移动到ObservableObject类:

final class UserData: ObservableObject {
    @Published var leftSide = Double()
    @Published var rightSide = Double()
    @Published var value1 = Double()
    @Published var value2 = Double()
}

I updated my main view with @ObservedObject var userData = UserData() so I could access the variables in my view and this works fine我用@ObservedObject var userData = UserData()更新了我的主视图,这样我就可以访问视图中的变量,这很好用

Now I want to update the value1 and value2 variables when the other changes.现在我想在其他更改时更新 value1 和 value2 变量。 From the reading I have done I think I need to use a subscriber but I cannot find anything on how to set this up with they @Published property.根据我所做的阅读,我认为我需要使用订阅者,但我找不到关于如何使用他们的@Published属性进行设置的任何信息。 Have I gone about this in the correct way or is there a better way to do this?我是否以正确的方式解决了这个问题,或者有更好的方法来做到这一点?

I have 4 main variables at the moment, the left and right side of the ratio, and the first and second value that will be calculated in ratio我目前有 4 个主要变量,比率的左侧和右侧,以及在比率中计算的第一个和第二个值

so, why you don't declare it as computed variables?那么,为什么不将其声明为计算变量?

final class UserData: ObservableObject {
    @Published var leftSide = 0.0
    @Published var rightSide = 0.0
    var value: Double {
        // perform the calculation here
        ...
    }
}

If you like to change value independently (bind it to SwiftUI component), and modify the value in case leftSide or rightSide are modified, use willSet or even didSet如果你喜欢独立改变值(绑定到 SwiftUI 组件),并且在修改 leftSide 或 rightSide 的情况下修改值,使用 willSet 甚至 didSet

final class UserData: ObservableObject {
    @Published var leftSide = 0.0 {
        willSet {
           // update value 
            value = f(newValue)
        }
        didSet {
           // update value
           value = f(oldValue)
        }
    }
    @Published var rightSide = 0.0
    @Published var value = 0.0
    func f(_ v: Double)->Double {
        ......
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SwiftUI:当 state 变量更改时,视图不会更新 - SwiftUI: View does not update when state variable changes SwiftUI:@State 变量在其值更改时不更新视图(在同一视图中) - SwiftUI: @State variable does not update View when its value changes (within same View) SwiftUI 列表在查看 model @Published 更改时崩溃并出现“NSRangeException” - SwiftUI List crashes with 'NSRangeException' when view model @Published changes SwiftUI 发布视图 model 成员值更改时视图上的运行方法 - SwiftUI run method on view when Published view model member value changes iOS - SwiftUI 在位置更改时更新文本 - iOS - SwiftUI update text when location changes 更改 @State 变量不会更新 SwiftUI 中的视图 - Changing @State variable does not update the View in SwiftUI 当环境对象更新导致同一视图重新加载时,如何导航到另一个 SwiftUI 视图 - How do I navigate to another SwiftUI View when an Environment Object update is causing the same view to reload 拖动到 SwiftUI 中的另一个视图时删除视图 - Delete View when dragged to another View in SwiftUI 从另一个视图将变量传递给 SwiftUI AVPlayer - Pass variable to SwiftUI AVPlayer from another view SwiftUI:如何仅在需要时更新从父视图转换的变量? - SwiftUI: how to update a variable transiting from a parent view only when wanted?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM