回到SwiftUI中的特定视图(popToViewController) [英] Come back to a specific View in SwiftUI (popToViewController)
问题描述
在SwiftUI中是否可以返回到特定视图?假设我以这种方式有三种观点:
Is it possible in SwiftUI to come back to a specific view? Let's say I have three views this way:
struct View1: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: View2()) {
Text("Navigate to View2")
}
.navigationBarTitle("View1")
}
}
}
}
struct View2: View {
var body: some View {
NavigationLink(destination: View3()) {
Text("Navigate to View3")
}
.navigationBarTitle("View2")
}
}
struct View3: View {
var body: some View {
Text("View3!")
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
View1()
}
}
#endif
导航来回工作:
View1->View2->View3
View3->View2->View1
是否可以从View3
直接返回到View1
?我要寻找的是与UIKit
Is it possible to directly come back to View1
from the View3
? What I'm looking for is something similar to the UIKit
func popToViewController(_ viewController: UIViewController,
animated: Bool) -> [UIViewController]?
推荐答案
试图解决此问题,我最终创建了一个名为swiftui-navigation-stack
(
Trying to solve this issue I ended up creating an open source project called swiftui-navigation-stack
(https://github.com/biobeats/swiftui-navigation-stack). It contains the NavigationStackView
, a view that mimics all the navigation behaviours of the standard NavigationView
, adding some other features (all the features are explained in the readme of the repo). To answer the question here above we can use the NavigationStackView
this way:
让我们假装我们必须实现这样的导航:
Let's pretend we have to implement a navigation like this:
View1 (push)-> View2 (push)-> View3 (push)-> View4 (pop)-> View2
首先将您的第一个视图嵌入到NavigationStackView
中(就像使用标准NavigationView
一样):
First of all embed your first view in a NavigationStackView
(as you'd do with the standard NavigationView
):
struct RootView: View {
var body: some View {
NavigationStackView {
View1()
}
}
}
让我们创建以下简单视图以构建示例:
Let's create these simple views to build the example:
struct View1: View {
var body: some View {
ZStack {
Color.yellow.edgesIgnoringSafeArea(.all)
VStack {
Text("VIEW 1")
Spacer()
PushView(destination: View2(), destinationId: "view2") {
Text("PUSH TO VIEW 2")
}
}
}
}
}
struct View2: View {
var body: some View {
ZStack {
Color.green.edgesIgnoringSafeArea(.all)
VStack {
Text("VIEW 2")
Spacer()
PushView(destination: View3()) {
Text("PUSH TO VIEW 3")
}
}
}
}
}
struct View3: View {
var body: some View {
ZStack {
Color.gray.edgesIgnoringSafeArea(.all)
VStack {
Text("VIEW 3")
Spacer()
PushView(destination: View4()) {
Text("PUSH TO VIEW 4")
}
}
}
}
}
struct View4: View {
var body: some View {
ZStack {
Color.white.edgesIgnoringSafeArea(.all)
VStack {
Text("VIEW 4")
Spacer()
PopView(destination: .view(withId: "view2")) {
Text("POP TO VIEW 2")
}
}
}
}
}
PushView
和PopView
允许您在视图之间导航,除其他外,它们还可以为视图指定标识符(以便您可以在需要时返回到它).
PushView
and PopView
let you navigate between views and, among other things, they let you specify an identifier for a view (so that you can come back to it if you need).
以下是完整的示例,您可以将其复制粘贴到xCode以便自己尝试:
The following is the complete example, you can copy-paste it to xCode to try it yourself:
import SwiftUI
import NavigationStack
struct RootView: View {
var body: some View {
NavigationStackView {
View1()
}
}
}
struct View1: View {
var body: some View {
ZStack {
Color.yellow.edgesIgnoringSafeArea(.all)
VStack {
Text("VIEW 1")
Spacer()
PushView(destination: View2(), destinationId: "view2") {
Text("PUSH TO VIEW 2")
}
}
}
}
}
struct View2: View {
var body: some View {
ZStack {
Color.green.edgesIgnoringSafeArea(.all)
VStack {
Text("VIEW 2")
Spacer()
PushView(destination: View3()) {
Text("PUSH TO VIEW 3")
}
}
}
}
}
struct View3: View {
var body: some View {
ZStack {
Color.gray.edgesIgnoringSafeArea(.all)
VStack {
Text("VIEW 3")
Spacer()
PushView(destination: View4()) {
Text("PUSH TO VIEW 4")
}
}
}
}
}
struct View4: View {
var body: some View {
ZStack {
Color.white.edgesIgnoringSafeArea(.all)
VStack {
Text("VIEW 4")
Spacer()
PopView(destination: .view(withId: "view2")) {
Text("POP TO VIEW 2")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
RootView()
}
}
结果是:
这篇关于回到SwiftUI中的特定视图(popToViewController)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!