Search code examples
swiftxcodeswiftuipicker

Why Picker in SwiftUI works just one time


I'm starting with SwiftUI and created this code

struct ContentView: View {

let countries = ["CZ","SK","DE"]
@State private var country = 0

var body: some View {
    NavigationView {
        VStack {
            NavigationLink(destination: SettingsView()) {
                Text("Settings")
            }
            Form {
                Picker("Pick a country", selection: $country) {
                    ForEach(0..<countries.count) { index in
                        Text(self.countries[index])
                    }
                }
                Text(countries[country])
            }
        }
        .navigationBarTitle("UNECE Data")
    }
}
}

if I select value from the Picker it properly returns value back to the Text but if I do the same action again list of values is not shown and XCode writes this to console window

TableView Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window.

I will appreciate any suggestion.


Solution

  • Yes, it is known bug, on real device it works. Not only you are annoying ...

    In your case (to be able to continue) your development, just click on "Settings", next go back and the picker is ready again. By the way, try to click on "Settings", go back and try it again. The link "Settings" has the same wrong behavior.

    Try Master / Detail template from apple, add only one Date and check, that it will work the same way. How it is possible? Swift runtime is not part of application anymore (due to ABI)

    It works on real devices from 13.3, last SDK on current Xcode is still 13.2. (check your project settings)

    Using different Picker style could help.