run*_*ter 5 swift swiftui swiftui-navigationlink
struct Testing: View {
var body: some View {
NavigationView{
VStack {
Text("View 1")
Text("View 1.3")
NavigationLink(destination: TestView(),
label: {
Text("View 1 navigation")
})
}
}
}
}
struct TestView: View {
var body: some View {
VStack {
Text("View 2")
}
.navigationBarItems(trailing:NavigationLink(
destination: Text("View 3"),
label: {
Text("Navigate")
}))
}
}
Run Code Online (Sandbox Code Playgroud)
当单击NavigationLinkTestViews 内部时navigationBarItems,它会导航到视图 3。当我单击后退按钮时,它不会返回到 TestView,而是会一直返回到测试。
如何让它返回到上一个视图而不是第一个视图?
我正在使用 Xcode 12。
将导航链接放入导航视图主体中(导航栏不在导航视图中)
所以这是可能的方法
struct TestView: View {
@State private var isActive = false
var body: some View {
VStack {
Text("View 2")
}
.background(
NavigationLink(destination: Text("View 3"), isActive: $isActive,
label: { EmptyView() })
)
.navigationBarItems(trailing:
Button("Navigate", action: { self.isActive = true }))
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1097 次 |
| 最近记录: |