swift - 选择器中的持久值在 SwiftUI 中更改 View

标签 swift forms picker combine

我不明白如何在 SwiftUI 中实现一个简单的选择器,显示一个值列表,这些值保留在不同 View 之间切换的所选值。顺便说一句,我可以使用选定的值通过 Combine 框架更新模型。

这是代码,但是 onAppear{}/onDisappear{} 没有按预期工作:

struct CompanyView: View {

    @ObservedObject var dataManager: DataManager = DataManager.shared

    @State var selTipoAzienda = 0

    var body: some View {
        VStack {
            companyPhoto
            Text("Company view")
            Form {
                Picker(selection: $selTipoAzienda, label: Text("Tipo Azienda")) {
                    ForEach(0 ..<  self.dataManager.company.tipoAziendaList.count) {
                        Text(self.dataManager.company.tipoAziendaList[$0])
                    }
                }
            }

            Button(action:  {self.dataManager.cambiaTipoAzienda(tipoAzienda: self.dataManager.company.tipoAziendaList[self.selTipoAzienda]) }) {
                Image(systemName: "info.circle.fill")
                    .font(Font.system(size: 28))
                    .padding(.horizontal, 16)
            }
        }
//        .onAppear{
//            self.selTipoAzienda = self.dataManager.company.tipoAziendaList.firstIndex(of: self.dataManager.company.tipoAzienda) ?? 0
//        }
//        .onDisappear{
//            self.dataManager.cambiaTipoAzienda(tipoAzienda: self.dataManager.company.tipoAziendaList[self.selTipoAzienda])
//        }
    }

我认为 binding 和 didSet 会是答案,但我不知道如何实现它们

最佳答案

提供的代码是不可编译/不可测试的,所以下面只是展示了一种方法(另见内联评论)

struct CompanyView: View {

    @ObservedObject var dataManager: DataManager = DataManager.shared
    @State var selTipoAzienda: Int

    init() {
        // set up initial value from persistent data manager
        _selTipoAzienda = State(initialValue: self.dataManager.company.tipoAziendaList.firstIndex(of: self.dataManager.company.tipoAzienda) ?? 0)
    }

    var body: some View {
        // inline proxy binding to intercept Picker changes
        let boundSelTipoAzienda = Binding(get: { self.selTipoAzienda }, set: {
            self.selTipoAzienda = $0

            // store selected value into data manager
            self.dataManager.cambiaTipoAzienda(tipoAzienda: self.dataManager.company.tipoAziendaList[$0])
        })

        return VStack {
            companyPhoto
            Text("Company view")
            Form {
                Picker(selection: boundSelTipoAzienda, label: Text("Tipo Azienda")) {
                    ForEach(0 ..<  self.dataManager.company.tipoAziendaList.count) {
                        Text(self.dataManager.company.tipoAziendaList[$0])
                    }
                }
            }

            Button(action:  {self.dataManager.cambiaTipoAzienda(tipoAzienda: self.dataManager.company.tipoAziendaList[self.selTipoAzienda]) }) {
                Image(systemName: "info.circle.fill")
                    .font(Font.system(size: 28))
                    .padding(.horizontal, 16)
            }
        }
    }
}

关于swift - 选择器中的持久值在 SwiftUI 中更改 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61699032/

相关文章:

ios - 用户默认值未给出值

ios - 折线图中未显示的数据点

ios - 不停止定时器,重新启动: will it cause running multiple timers in swift

objective-c - 如何使用 NSNumberFormatter 格式化 UITextField?

PHP _POST 问题毫无意义。值(value)观消失?

ios - 使用 react-native 设置选择器的高度

android - Appcompact DialogFragment单选复选标记颜色

javascript - 在新窗口中打开动态构建的 Google URL

forms - Spring 提交表单后的空字段

javascript - 使用 React-native-picker-select headless 组件