简体   繁体   English

swiftui更改显示的选择器值

[英]swiftui change shown picker value

Hello I have made a picker in swiftUI where the user has to accept the change on the picker.您好,我在 swiftUI 中创建了一个选择器,用户必须接受选择器上的更改。

It works well, but there is one issue.它运作良好,但有一个问题。 If the user selects something and then declines the change, the shown value will still be what was selected though the actual value doesn't change.如果用户选择了某些东西然后拒绝更改,则显示的值仍将是选择的值,尽管实际值没有改变。

If I open the picker again without selecting anything it reverts back to the right value but how to I do this without having to open the picker?如果我在没有选择任何内容的情况下再次打开选择器,它会恢复到正确的值,但是如何在不打开选择器的情况下执行此操作?

struct ContentView: View {
@State private var selectedColorIndex = "cd"
@State private var temp = ""
@State var alert = false

private var colors = ["ab", "cd", "ef", "gh", "ij", "kl", "mn", "op", "qr", "st", "uv", "wx", "yz"]
var body: some View {
    
    
    HStack {
        Text("Location")
        Picker("Location", selection: Binding(get: {selectedColorIndex}, set: {
            temp = $0
            alert = true
        })) {
            ForEach(colors, id: \.self) {
                Text($0)
            }
        }.pickerStyle(MenuPickerStyle())
        
    }.alert(isPresented: $alert) {
        Alert(title: Text("are you sure"), primaryButton: .default(Text("yes"), action: {
            selectedColorIndex = temp
            
            
        }), secondaryButton: .default(Text("no"), action: {
            
            //change shown value back to prev selectedColorIndex
        }))
    }
    
}

} }

This does not work because the PickerView does not refresh with the value it gets from the binding.这不起作用,因为 PickerView 不会使用从绑定中获取的值进行刷新。 Adding a print statement in the binding confirms, that it indeed delivers the correct value.在绑定中添加打印语句确认它确实提供了正确的值。

A simple solution to this would be to force the Picker to refresh itself every time the body of the ContentView gets reavaluated.一个简单的解决方案是在每次重新评估ContentView的主体时强制 Picker 刷新自身。 We can achieve this by manually changing the id of the Picker .我们可以通过手动更改Pickerid来实现这一点。

var body: some View {
    HStack {
        Text("Location")
        Picker("Location", selection: Binding(get: {selectedColorIndex}, set: {
            temp = $0
            alert = true
        })) {
            ForEach(colors, id: \.self) {
                Text($0)
            }
        }
        .id(UUID()) // add this
        .pickerStyle(MenuPickerStyle())
            
        
    }.alert(isPresented: $alert) {
        Alert(title: Text("are you sure"), primaryButton: .default(Text("yes"), action: {
            selectedColorIndex = temp
            
            
        }), secondaryButton: .default(Text("no"), action: {
            // No code needed here
            //change shown value back to prev selectedColorIndex
        }))
    }
}

If you want to know more why this works you can read my answer here: more info如果您想了解更多为什么这样有效,您可以在此处阅读我的答案:更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM