[英]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.我在 SwiftUI Apple Watch 应用程序中进行导航时遇到问题。 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.我主要了解如何创建 SwiftUI 视图,但我不确定从 SwiftUI 视图导航到另一个 SwiftUI 视图的最佳方式是什么。
Is there a way to push a user to another SwiftUI screen when they tap a button?有没有办法在用户点击按钮时将用户推送到另一个 SwiftUI 屏幕?
To provide a very basic example how it can work 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)
)
}
}
} }
You can do this by initializing a NavigationLink with your destination view.您可以通过使用目标视图初始化NavigationLink来实现此目的。 Its documentation is minimal at the moment, but the tutorial Building Lists and Navigation and WWDC session SwiftUI on watchOS may be of help.
目前它的文档很少,但watchOS 上的教程构建列表和导航以及WWDC 会话 SwiftUI可能会有所帮助。
Just replace NavigationView by List, and it works like a charm只需将 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.