如何在 SwiftUI Apple Watch App 中从一个屏幕导航到另一个屏幕

How to navigate from one screen to another in SwiftUI Apple Watch App

我在 SwiftUI Apple Watch 应用程序中进行导航时遇到问题。关于此的在线资源很少。

我基本上了解如何创建 SwiftUI 视图,但我不确定从 SwiftUI 视图导航到另一个 SwiftUI 视图的最佳方式是什么。

有没有办法在用户点击按钮时将其推送到另一个 SwiftUI 屏幕?

您可以通过初始化来做到这一点 NavigationLink with your destination view. Its documentation is minimal at the moment, but the tutorial Building Lists and Navigation and WWDC session SwiftUI on watchOS 可能会有帮助。

提供一个非常基本的示例,说明它如何在 watchOS 上运行。

struct ListView: View {
var body: some View {



    List{

        RowView(title: "Row 1")
        RowView(title: "Row 2")
        RowView(title: "Row 3")
        RowView(title: "Row 4")

    }
    .listStyle(.carousel)


    }
}
struct RowView: View {
    let title: String
    var body: some View {

    NavigationLink(destination: Text("Detail \(title)")) {

        Text(title)
            .frame(height: 80, alignment: .topTrailing)
            .listRowBackground(
                Color.blue
                    .cornerRadius(12)
        )
    }
}

}

只需将 NavigationView 替换为 List,效果非常好

struct ContentView : View{
    var body : some View {
        List{
            VStack{
        NavigationLink(destination: BPView()) {
            Text("Some text")
                .foregroundColor(Color.blue);
        }
            NavigationLink(destination:myView()) {
                Text("Show text here")
                .foregroundColor(Color.blue);
            }
            }}

    }
}