簡體   English   中英

選擇器中的持久值更改 SwiftUI 中的視圖

[英]persistent value in a picker changing views in SwiftUI

我不明白如何在 SwiftUI 中實現一個簡單的選擇器,該選擇器顯示一個值列表,該列表保留在不同視圖之間切換的選定值。 順便說一句,我可以使用選定的值通過組合框架更新 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])
//        }
    }

我認為 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)
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM