Sorry for the title and length I struggled to summerise
I have a class of @ObserableObject @Published data which I want to change and use the new data on a second page accessed via NavigationLink
class Data: ObservableObject {
@Published var number = 5
var correctNumber: Int {
number + 1
}
}
I then have a Picker to choose/change the number
Picker("What times tables do you want to test your knowledge on?", selection: $data.number) {
ForEach(0 ..< data.numberRange.count) {
Text("\(self.data.numberRange[$0])")
}
}
This works as expected when I have the Text show the corrected number on the main screen and changes/updates when the picker is changed
Text ("Number = \(data.correctNumber)")
I then move to a second screen with NavigationLink
NavigationLink(destination: QuestionView()) {
Text("Start")
}
On the second screen, I set the @ObservedObject and have the text display again.
struct QuestionView: View {
@ObservedObject var data = Data()
var body: some View {
Text("Number = \(self.data.correctNumber)")
}
When pressing Start it only shows the number as 5 + 1 and not the number it changed to using the picker.
I have tried;
- Not using self. but there was no noticeable difference
- Swapping to a binding $ and got the following issues; Cannot assign to property: 'correctNumber' is a get-only property and Instance method 'appendInterpolation' requires that 'Binding' conform to '_FormatSpecifiable'
- Adding @Published to correctNumber but I cannot add that to a computed state
Now I am out of ideas, can anyone help a noob, please
PS I am sorry if the terminology is wrong, feel free to correct me so I learn for next time