在 Apple Watch 平台的 SwiftUI 中使用 NavigationStack 时出现以下错误。
NavigationView 效果很好,但没有达到目的。
不确定发生了什么问题。
如有任何帮助,我们将不胜感激。
Xcode:15.4 目标:Apple Watch OS 10.4
<NavigationHostingControllerCache>: MISS at depth 0 in free stack
[NavigationHostingControllerCache_UIKit] <_TtGC7SwiftUI32NavigationStackHostingControllerVS_7AnyView_: 0x1540f200> containment skipped because source and destination were `nil`
<NavigationHostingControllerCache>: HIT at depth 0 in free stack
<NavigationHostingControllerCache>: MISS at depth 1 in free stack
<NavigationHostingControllerCache>: MISS at depth 2 in free stack
[NavigationHostingControllerCache_UIKit] <_TtGC7SwiftUI32NavigationStackHostingControllerVS_7AnyView_: 0x1540f200> containment skipped because sourceNavigationController and destination were equal Optional(<SwiftUI.UIKitNavigationController: 0x14c18200>)
[NavigationHostingControllerCache_UIKit] <_TtGC7SwiftUI32NavigationStackHostingControllerVS_7AnyView_: 0x15424400> containment skipped because sourceNavigationController or destination were nil
[NavigationHostingControllerCache_UIKit] <_TtGC7SwiftUI32NavigationStackHostingControllerVS_7AnyView_: 0x15432800> containment skipped because sourceNavigationController or destination were nil
代码:
import SwiftUI
@main
struct WearableBankingApp: App {
var body: some Scene {
WindowGroup {
ContentView1()
}
}
}
struct ContentView1: View {
@State var path = NavigationPath(["1", "2"])
var body: some View {
NavigationStack(path: $path) {
VStack {
NavigationLink("Go to int screen", value: "1")
}
.navigationDestination(for: String.self) { value in
Text("This is a string screen with value: \(value)")
}
}
}
}
看起来下面的代码可能更符合您的要求?
struct ContentView1: View {
var destinations = ["1", "2"]
@State var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
VStack {
ForEach(destinations) { value in
NavigationLink("Go to int screen", value: value)
}
}
.navigationDestination(for: String.self) { value in
Text("This is a string screen with value: \(value)")
}
}
}
}