While playing around with NavigationStack and onAppear I noticed the following message appears in my console when navigating back to a view that focuses on a field with .onAppear
.
Here is the code:
import SwiftUI
class VM: ObservableObject {
@Published var path: [String] = []
}
struct ContentView: View {
@FocusState var focus
@State var text = ""
@StateObject var vm = VM()
var body: some View {
NavigationStack(path: $vm.path) {
VStack {
TextField("text", text: $text)
.focused($focus)
Button ("next") {
vm.path.append("next")
debugPrint(vm.path)
}
}
.onAppear {
debugPrint("appear")
focus = true
}
.navigationDestination(for: String.self) { dest in
switch dest {
case "next":
v2(path: $vm.path)
.onDisappear {
debugPrint("disappear")
}
case "next2":
EmptyView()
default:
EmptyView()
}
}
}
}
}
struct v2: View {
@FocusState var focus
@State var text = ""
@Binding var path: [String]
var body: some View {
Text("hello")
VStack {
TextField("text", text: $text)
.focused($focus)
Button ("next") {
path.append("next2")
}
}
.onAppear {
debugPrint("appear")
focus = true
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
And here is the console output:
"appear"
["next"]
"appear"
"appear"
"disappear"
Update NavigationAuthority bound path tried to update multiple times per frame.
I am using the latest XCode that is non beta, and the app is built for iOS 16.4+. Is there a way to resolve this issue and make it stop displaying this message or is it a problem with SwiftUI itself?