SwiftUI 中是否可以返回到特定 View ?假设我这样有三种观点:
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
func popToViewController(_ viewController: UIViewController,
animated: Bool) -> [UIViewController]?
最佳答案
为了解决这个问题,我最终创建了一个名为 swiftui-navigation-stack
( https://github.com/biobeats/swiftui-navigation-stack ) 的开源项目。它包含 NavigationStackView
,这是一个模仿标准 NavigationView
的所有导航行为的 View ,并添加了一些其他功能(所有功能均在存储库的自述文件中进行了解释)。要回答上面的问题,我们可以这样使用 NavigationStackView
:
假设我们必须实现这样的导航:
View1 (push)-> View2 (push)-> View3 (push)-> View4 (pop)-> View2
首先将您的第一个 View 嵌入 NavigationStackView
中(就像使用标准 NavigationView
一样):
struct RootView: View {
var body: some View {
NavigationStackView {
View1()
}
}
}
让我们创建这些简单的 View 来构建示例:
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
让您可以在 View 之间导航,除此之外,它们还可以让您指定 View 的标识符(以便您可以在以下情况下返回该 View ):你需要)。
下面是完整的例子,你可以复制粘贴到xCode中自己尝试一下:
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()
}
}
结果是:
关于ios - 返回 SwiftUI 中的特定 View (popToViewController),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57700532/