Oh the code seem to have broken while copy & paste.
Here's the code for 100% reproduction
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationStack {
Page1()
.environmentObject(Foo())
}
}
}
class Foo: ObservableObject {
init() {}
}
struct Page1: View {
@EnvironmentObject
private var viewModel: Foo
@Binding
var value: Int
init() {
self._value = .init(get: { 0 }, set: { _ in })
}
@State
private var id: UUID? = nil
var body: some View {
NavigationLink(value: 0) {
Text("Go to page2")
.padding()
.foregroundStyle(.white)
.background(.black)
.cornerRadius(12)
}
.navigationDestination(for: Int.self) { _ in
Page2(
onComplete: {
print("value", value)
}
)
}
}
}
struct Page2: View {
public init(onComplete _: @escaping() -> Void) {}
public var body: some View {
Text("This is Page2")
.task {
print("hi")
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: