SwiftUI: Picker content not refreshed when adding Element

随声附和 提交于 2019-12-24 00:28:15

问题


I have a Picker Element in a VStack, but when its content changes by adding a new Element, the Picker does not refresh.

After hiding and showing the Picker, the new Element is visible.

Does anybody have any idea how to refresh the content of a Picker, without needing to hide / show it?

You can reproduce this by creating a new SwiftUI project and copying the following code instead of the "ContentView" struct.

class ContentModel {
    @Published var pickerData: [String] = ["1"]

    func addPickerData() {
        pickerData.append("\(pickerData.count + 1)")
    }
}

struct ContentView: View {
    let contentModel = ContentModel()

    @State private var showPicker = false
    @State private var selectedPickerValue = ""

    var body: some View {
        VStack(spacing: 8) {
            Text("Adding a new Element to the Picker does not refresh its content :-(")
            Button(action: {
                self.contentModel.addPickerData()
            }) {
                Text("Add Picker Data")
            }
            Button(action: {
                self.showPicker.toggle()
            }) {
                Text("Show / Hide 2nd Picker")
            }
            Picker("Select",selection: $selectedPickerValue) {
                ForEach(contentModel.pickerData, id: \.self) { data in
                    Text(data)
                }
            }
            if (showPicker) {
                Picker("Select",selection: $selectedPickerValue) {
                    ForEach(contentModel.pickerData, id: \.self) { data in
                        Text(data)
                    }
                }
            }
            Text("Selected Value: \(selectedPickerValue)")
        }
    }
}

Thanks in advance for any help!


回答1:


Here is the trick of reactive and always use two copies of same thing when you need to refresh something.

 class ContentModel{
 @Published var pickerData: [String] = ["1"]

   func addPickerData() {
       pickerData.append("\(pickerData.count + 1)")
   }

}

struct ContentSSView: View {
let contentModel = ContentModel()

@State private var showPicker = false
@State private var selectedPickerValue = ""

var body: some View {
    VStack(spacing: 8) {
        Text("Adding a new Element to the Picker does not refresh its content :-(")
        Button(action: {
            self.contentModel.addPickerData()
             self.showPicker.toggle()
        }) {
            Text("Add Picker Data")
        }
        Button(action: {
            self.showPicker.toggle()
        }) {
            Text("Show / Hide 2nd Picker")
        }

        if (showPicker) {
            Picker("Select",selection: $selectedPickerValue) {
                ForEach(contentModel.pickerData, id: \.self) { data in
                    Text(data)
                }
            }
        }else{
            Picker("Select",selection: $selectedPickerValue) {
                ForEach(contentModel.pickerData, id: \.self) { data in
                    Text(data)
                }
            }
        }
        Text("Selected Value: \(selectedPickerValue)")
    }
}

}




回答2:


I have a GitHub repo with this issue. I don't think having two Pickers is a viable solution.

Picker Update Bug GitHub Repo



来源:https://stackoverflow.com/questions/58660945/swiftui-picker-content-not-refreshed-when-adding-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!