I have a problem with auto view poping
pushed view via Navigation Link.
On my ContentView
is a list which can be changed after a few seconds, but if I push a new view before it, new view pops automatically after new data appears. It is unexpected behaviour. Is it a SwiftUI 3 bug or my mistake? Problem on iOS 15
My View Model
class ViewModel: ObservableObject {
@Published var list: [Int] = [1, 2, 3, 4, 5, 6, 7, 8]
func changeAfterTime() {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { [weak self] in
self?.list = [9,10,11]
}
}
}
My ContentView
struct ContentView: View {
@EnvironmentObject var viewModel: ViewModel
@State private var selection: Int?
var body: some View {
NavigationView {
List(viewModel.list, id: \.self) { element in
VStack {
Text(element, format: .number)
NavigationLink("", tag: element, selection: $selection) {
TestView()
}
.opacity(.zero)
}
}
.onAppear {
viewModel.changeAfterTime()
}
}
}
}
struct TestView: View {
var body: some View {
Text("Wait")
}
}