Picker inside toolbar does not update selection properly

I have a simple List inside a NavigationView with a toolbar that contains a Picker. When I select an option in the picker, the variable value changes, but when I click again on the picker it shows the default variable value. This does not seems to happen when I put the picker on another container, for example directly in the List, so it seems to be a problem with the toolbar itself.

Here is my code:

struct ExampleView: View {
    //this is a enum that conforms Equatable, Hashable, CaseIterable
    @State private var sort: MediaListSort = .updatedTimeDesc

    var body: some View {
        List {
           //...
        }
        .toolbar {
            ToolbarItem {
                Picker("Sort", selection: $sort) {
                    Text("Title").tag(MediaListSort.mediaTitleNativeDesc)
                    Text("Score").tag(MediaListSort.scoreDesc)
                    Text("Last Updated").tag(MediaListSort.updatedTimeDesc)
                    Text("Last Added").tag(MediaListSort.addedTimeDesc)
                }
            }
        }
    }
}

Behaviour:

I am currently using the latest Xcode 14 beta 04, so I am not sure if this issue is related to that.

Answered by axiel7 in 725734022

It seems it has been resolved in the latest iOS 16 beta 8 and Xcode 14 beta 6

I am seeing the same issue and I have yet to find a fix. I'm assuming it is a bug with the new beta.

Problem still present on Xcode 14 beta 5

I have the same problem on Xcode 14 beta.. but it behaves correctly when I run it using Xcode 13, so it must be a bug in Xcode 14 beta

Unfortunately still happening in Beta 6. Anyone find any other workarounds? I submitted a radar (FB11367854) using the following example. Sucks because I doubt it is going to be fixed before the RC xcode 14 release, hopefully it can get into 14.1. Looks like I may have to my change design or not support iOS 16 features for now.


import SwiftUI

enum Setting: String, CaseIterable {
    case first = "First"
    case second = "Second"
    case third = "Third"
}

struct ContentView: View {
    @State var setting: Setting = .first

    var body: some View {
        NavigationView {
            VStack {
                Text("Setting: \(setting.rawValue)")
                /// Picker works when in a normal view
                Picker("Change Setting", selection: $setting) {
                    ForEach(Setting.allCases, id: \.self) { setting in
                        Text(setting.rawValue).tag(setting)
                    }
                }
            }
            .padding()
            .navigationTitle("ToolbarBug")
            .toolbar {
                ToolbarItem {
                    Menu("Test") {
                        // This display of the setting also never changes
                        Text("Current setting: \(setting.rawValue)")
                        // Picker within a toolbar item does not update it's selection checkmark
                        Picker("Change Setting", selection: $setting) {
                            ForEach(Setting.allCases, id: \.self) { setting in
                                Text(setting.rawValue).tag(setting)
                            }
                        }
                    }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Accepted Answer

It seems it has been resolved in the latest iOS 16 beta 8 and Xcode 14 beta 6

Picker inside toolbar does not update selection properly
 
 
Q