Это пример кода, который упрощает мою проблему есть:
Код: Выделить всё
struct ArrayDisplayWithPageNumber: View {
var arrayDisplay: ArrayDisplay
@Binding var index: Int // This should update when 'index' changes in 'arrayDisplay'.
init() {
self.arrayDisplay = ArrayDisplay()
self._index = self.arrayDisplay.$index
}
var body: some View {
VStack {
arrayDisplay
.padding()
Text("Page number: \(index + 1)") // Stays at "Page number: 1".
}
}
}
Код: Выделить всё
struct ArrayDisplay: View {
var greetings: [String] = ["Hello", "Hola", "Bonjour"]
@State var index: Int = 0 // This is the property I want to read in the parent view.
var body: some View {
VStack {
Button("Next greeting") {
if self.index + 1 == self.greetings.count {
self.index = 0 // Simple check to prevent array out of bounds error.
} else {
self.index += 1 // Increments index if it's not at it's highest index value.
}
}
Text(greetings[index])
.font(.title)
}
}
}
код> обновить, когда он изменится?
Подробнее здесь: https://stackoverflow.com/questions/622 ... parent-vie