简体   繁体   中英

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

I'm having problems doing navigation in a SwiftUI Apple Watch app. There's minimal resources online regarding this.

I mostly understand how to create SwiftUI views, but I'm not sure what the best way to navigate from SwiftUI view to another SwiftUI view is.

Is there a way to push a user to another SwiftUI screen when they tap a button?

To provide a very basic example how it can work on 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)
        )
    }
}

}

You can do this by initializing a 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 may be of help.

Just replace NavigationView by List, and it works like a charm

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);
            }
            }}

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM