У меня Picker
встроено в Form
внутри NavigationView
. Я хотел бы иметь отдельный текст для выбранного элемента в основном View
и более подробные описания при выборе элементов в сборщике View
.
Это то, что я пробовал до сих пор:
struct Item {
let abbr: String
let desc: String
}
struct ContentView: View {
@State private var selectedIndex = 0
let items: [Item] = [
Item(abbr: "AA", desc: "aaaaa"),
Item(abbr: "BB", desc: "bbbbb"),
Item(abbr: "CC", desc: "ccccc"),
]
var body: some View {
NavigationView {
Form {
picker
}
}
}
var picker: some View {
Picker(selection: $selectedIndex, label: Text("Chosen item")) {
ForEach(0..<items.count) { index in
Group {
if self.selectedIndex == index {
Text(self.items[index].abbr)
} else {
Text(self.items[index].desc)
}
}
.tag(index)
}
.id(UUID())
}
}
}
Текущее решение
Это средство выбора на главном экране:
And this is the selection view:
The problem is that with this solution in the selection view there is "BB" instead of "bbbbb".
This occurs because the "BB" text in both screens is produced by the very same Text
view.
Expected result
The picker in the main view:
And in the selection view:
Ожидаемый результат
Возможно ли в SwiftUI иметь отдельные тексты (представления) для обоих экранов?