为什么这个 SwiftUI Picker 代码不起作用?
Why does this SwiftUI Picker code not work?
Xcode: 11.2.1
下面的代码适用于包含两个组件的简单表单;一个选择器(select 一个字母)和一个文本(显示 selected 字母)。代码编译并 运行s 但是,当字母被 selected 时,它不会出现在 "Selected..." 文本中。此外,Xcode 显示一个(虚假的?)运行 时间警告第一次(仅)一个字母是 selected..
struct ContentView: View {
@State private var letter = ""
private let letters = ["Alpha", "Bravo", "Charlie"]
var body: some View {
NavigationView {
Form {
Picker("Select a letter", selection: $letter) {
ForEach(0..<letters.count) {
Text(self.letters[[=10=]])
}
}
Text("Selected letter: \(letter)")
}
.navigationBarTitle("Main Menu")
}
}
}
有关信息:运行时间警告:
[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. Table view:
<_TtC7SwiftUIP33_BFB370BA5F1BADDC9D83021565761A4925UpdateCoalescingTableView:
0x7f8b33872600; baseClass = UITableView; frame = (0 0; 414 896);
clipsToBounds = YES; autoresize = W+H; gestureRecognizers = ; layer = ; contentOffset: {0,
-140}; contentSize: {414, 205.33333333333337}; adjustedContentInset: {140, 0, 34, 0}; dataSource:
<_TtGC7SwiftUIP10f5cd23419ListCoreCoordinatorGVS_20SystemListDataSourceOs5Never_GOS_19SelectionManagerBoxS2___:
0x7f8b33407a70>>
Picker 中的 ForEach 类型应与选择类型对齐。
这是一个应该适合您的更正代码:
@State private var letter = ""
private let letters = ["Alpha", "Bravo", "Charlie"]
var body: some View {
NavigationView {
Form {
Picker("Select a letter", selection: $letter) {
ForEach(letters, id: \.self) { option in
Text(option)
}
}
Text("Selected letter: \(letter)")
}
.navigationBarTitle("Main Menu")
}
}
郑重声明,我根据上面接受的答案以及我因该答案而增加的知识来回答我自己的问题。在这个问题中,我不是从数组中选择一个字母,而是数组中字母的 index 。因此,基于洞察力,替代的工作解决方案可能是:
struct ContentView: View {
@State private var letterIndex = 0
private let letters = ["Alpha", "Bravo", "Charlie"]
var body: some View {
NavigationView {
Form {
Picker("Select a letter", selection: $letterIndex) {
ForEach(0..<letters.count) { index in
Text(self.letters[index])
}
}
Text("Selected letter: \(letters[letterIndex])")
}
.navigationBarTitle("Main Menu")
}
}
Xcode: 11.2.1
下面的代码适用于包含两个组件的简单表单;一个选择器(select 一个字母)和一个文本(显示 selected 字母)。代码编译并 运行s 但是,当字母被 selected 时,它不会出现在 "Selected..." 文本中。此外,Xcode 显示一个(虚假的?)运行 时间警告第一次(仅)一个字母是 selected..
struct ContentView: View {
@State private var letter = ""
private let letters = ["Alpha", "Bravo", "Charlie"]
var body: some View {
NavigationView {
Form {
Picker("Select a letter", selection: $letter) {
ForEach(0..<letters.count) {
Text(self.letters[[=10=]])
}
}
Text("Selected letter: \(letter)")
}
.navigationBarTitle("Main Menu")
}
}
}
有关信息:运行时间警告:
[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. Table view: <_TtC7SwiftUIP33_BFB370BA5F1BADDC9D83021565761A4925UpdateCoalescingTableView: 0x7f8b33872600; baseClass = UITableView; frame = (0 0; 414 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = ; layer = ; contentOffset: {0, -140}; contentSize: {414, 205.33333333333337}; adjustedContentInset: {140, 0, 34, 0}; dataSource: <_TtGC7SwiftUIP10f5cd23419ListCoreCoordinatorGVS_20SystemListDataSourceOs5Never_GOS_19SelectionManagerBoxS2___: 0x7f8b33407a70>>
Picker 中的 ForEach 类型应与选择类型对齐。
这是一个应该适合您的更正代码:
@State private var letter = ""
private let letters = ["Alpha", "Bravo", "Charlie"]
var body: some View {
NavigationView {
Form {
Picker("Select a letter", selection: $letter) {
ForEach(letters, id: \.self) { option in
Text(option)
}
}
Text("Selected letter: \(letter)")
}
.navigationBarTitle("Main Menu")
}
}
郑重声明,我根据上面接受的答案以及我因该答案而增加的知识来回答我自己的问题。在这个问题中,我不是从数组中选择一个字母,而是数组中字母的 index 。因此,基于洞察力,替代的工作解决方案可能是:
struct ContentView: View {
@State private var letterIndex = 0
private let letters = ["Alpha", "Bravo", "Charlie"]
var body: some View {
NavigationView {
Form {
Picker("Select a letter", selection: $letterIndex) {
ForEach(0..<letters.count) { index in
Text(self.letters[index])
}
}
Text("Selected letter: \(letters[letterIndex])")
}
.navigationBarTitle("Main Menu")
}
}