Why is my view going back to the parent when the ObservedObject updates? I understand that the update to it redraws the view (and presumably also the ParentView) but frankly atm that I am looking at the ChildView I don't care about the look of the ParentView.
I would understand the behaviour if the displayed stuff was coming directly from the ParentView but it is tied to a class. Is this intended behaviour from Apple?
How am I supposed to manipulate Data in a View that got called by a NavigationLink without popping the view?
Why is this pop forced? Is there a way to "hold it back"?
I've tried something like this:
SwiftUI: @ObservedObject redraws every view but it didn't help. It just made it worse as I tap remove items anymore.
I have a code construct like this:
Parent View:
struct ContentView: View {
@ObservedObject var data = Data()
var body: some View {
NavigationView {
VStack() {
ForEach(self.data.myArray, id: \.self) { subArray in
NavigationLink(destination: Child(data: self.data, mySubArray: subArray)) {
Text(subArray[0]).padding().background(Color.yellow)
}
}
}
}
}
}
Child:
struct Child: View {
@ObservedObject var data: Data
var mySubArray: Array<String>
var body: some View {
ForEach(self.mySubArray, id: \.self) { str in
Text(str).padding().background(Color.yellow)
.onTapGesture {
self.data.removeFromSubArray(index: self.data.myArray.firstIndex(of: self.mySubArray) ?? 0, remove: str)
}
}
}
}
And an ObservableObject:
class Data: ObservableObject {
@Published var myArray: Array<Array<String>> = []
init() {
self.myArray.append(["A", "B", "C"])
self.myArray.append(["D", "E", "F"])
self.myArray.append(["G", "H", "I"])
}
func removeFromSubArray(index: Int, remove: String) {
if self.myArray.count-1 >= index {
if let subIndex = self.myArray[index].firstIndex(of: remove) {
self.myArray[index].remove(at: subIndex)
}
}
}
}