簡體   English   中英

如何在 SwiftUI Apple Watch App 中從一個屏幕導航到另一個屏幕

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

我在 SwiftUI Apple Watch 應用程序中進行導航時遇到問題。 網上關於這方面的資源很少。

我主要了解如何創建 SwiftUI 視圖,但我不確定從 SwiftUI 視圖導航到另一個 SwiftUI 視圖的最佳方式是什么。

有沒有辦法在用戶點擊按鈕時將用戶推送到另一個 SwiftUI 屏幕?

提供一個非常基本的示例,它如何在 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)
        )
    }
}

}

您可以通過使用目標視圖初始化NavigationLink來實現此目的。 目前它的文檔很少,但watchOS 上的教程構建列表和導航以及WWDC 會話 SwiftUI可能會有所幫助。

只需將 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);
            }
            }}

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM