如何以编程方式从Picker视图中删除所有项目

时间:2020-02-18 18:03:05

标签: swiftui picker

我的SwiftUI应用中有以下代码

struct ContentView: View {

@State private var selectedCountry: Country?
@State private var showSetting = false

@FetchRequest(entity: Country.entity(),
              sortDescriptors: [NSSortDescriptor(keyPath: \Country.cntryName, ascending: true)]
) var countries: FetchedResults<Country>

var body: some View {
    NavigationView {
        VStack {
            Form {
                Picker("Pick a country", selection: $selectedCountry) {
                    ForEach(countries, id: \Country.cntryName) { country in
                        Text(country.cntryName ?? "Error").tag(country as Country?)
                    }
                }
                if selectedCountry != nil {
                    DetailView(cntryName: (selectedCountry?.cntryName!)!)
                }
            }
        }
        .navigationBarTitle("UNECE Data")
        .navigationBarItems(trailing: Button("Settings", action: {
            self.showSetting.toggle()
        }))
    }
    .sheet(isPresented: $showSetting) {
        SettingsView(showSetting: self.$showSetting)
    }
}
}

我在SettingView中更新了CoreData Country实体,一旦应用回到ContentView中,我想从Picker中删除所有项目并加载新数据。在Picker中重复项目上方的代码-将新项目添加到旧集中。

0 个答案:

没有答案