为什么 SwiftUI 中的 Picker 只能工作一次

Why Picker in SwiftUI works just one time

我从 SwiftUI 开始并创建了这段代码

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")
    }
}
}

如果我 select 来自选择器的值它正确地 returns 值返回到文本但是如果我再次执行相同的操作则不会显示值列表并且 XCode 写这个安慰 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.

我将不胜感激任何建议。

这是 iOS13 中引入的错误,显然只影响物理设备。将您的 iOS 系统更新到 13.3,它在真实 iPhone 上运行良好,但警告仍然存在

是的,这是已知的错误,在真实设备上它可以工作。不仅你烦...

在您的情况下(为了能够继续)您的开发,只需单击 "Settings",然后返回,选择器再次准备就绪。顺便试试点击"Settings",回去再试试。 link "Settings" 有同样的错误行为。

试试 apple 的 Master / Detail 模板,只添加一个 Date 并检查,它会以同样的方式工作。怎么可能? Swift 运行时不再是应用程序的一部分(由于 ABI)

从 13.3 开始它可以在真实设备上运行,当前 Xcode 上的最后一个 SDK 仍然是 13.2。 (检查您的项目设置)

使用不同的选择器样式可能会有所帮助。