在SwiftUI中改变视图的拾取器中的持久性值。

问题描述 投票:0回答:1

我不明白如何在SwiftUI中实现一个简单的选取器,显示一个值的列表,在不同的视图之间切换保留所选的值。顺便说一下,我可以通过Combine框架使用选定的值来更新Model。

这里是代码,但是 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])
//        }
    }

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

swift forms picker combine
1个回答
0
投票

所提供的代码是不可编译测试的,所以下面只是展示了一个方法(也请看内联注释)。

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)
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.