为什么SwiftUI中的Picker只能运行一次 [英] Why Picker in SwiftUI works just one time

查看:565
本文介绍了为什么SwiftUI中的Picker只能运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如果我从选择器中选择值,它将正确地将值返回给文本,但是如果我再次执行相同的操作,则不会显示值列表,并且XCode会将其写入控制台窗口

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仅警告一次:告诉UITableView布置其 可见的单元格和其他内容,而不在视图层次结构中 (表视图或其父视图之一尚未添加到 窗户).通过强制将表视图内的视图强制为 在没有准确信息的情况下加载和执行布局(例如,表格视图 边界,特征收集,布局边距,安全区域插图等),以及 由于额外的布局,还将导致不必要的性能开销 通过.在以下位置建立象征性的断点 UITableViewAlertForLayoutOutsideViewHierarchy可以在 调试器,看看是什么原因导致这种情况的发生,因此您可以避免这种情况 如果可能的话,请完全采取行动,或将其推迟到表格视图中 已添加到窗口中.

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.

任何建议我都会感激.

推荐答案

是的,这是已知的错误,可以在实际设备上运行.不仅您烦人...

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.

尝试从Apple获得的Master/Detail模板,仅添加一个Date并进行检查,以确保其工作方式相同.怎么可能呢? Swift运行时不再是应用程序的一部分(由于ABI)

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)

它可在13.3以上的实际设备上运行,当前Xcode上的最新SDK仍为13.2. (检查您的项目设置)

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

使用不同的Picker样式可能会有所帮助.

Using different Picker style could help.

这篇关于为什么SwiftUI中的Picker只能运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆