SwiftUI:如何弹出到根视图 [英] SwiftUI: How to pop to Root view
问题描述
终于在 Beta 5 中,我们可以以编程方式弹出到父视图.但是,在我的应用程序中有几个地方,一个视图有一个保存"按钮,它可以结束一个多步骤的过程并返回到开头.在 UIKit 中,我使用 popToRootViewController(),但我一直无法找到在 SwiftUI 中执行相同操作的方法.
Finally now with Beta 5 we can programmatically pop to a parent View. However, there are several places in my App where a View has a "Save" button that concludes a several step process and returns to the beginning. In UIKit, I use popToRootViewController(), but I have been unable to figure out a way to do the same in SwiftUI.
下面是我试图实现的模式的一个简单示例.有什么想法吗?
Below is a simple example of the pattern I'm trying to achieve. Any ideas?
import SwiftUI
struct DetailViewB: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
VStack {
Text("This is Detail View B.")
Button(action: { self.presentationMode.value.dismiss() } )
{ Text("Pop to Detail View A.") }
Button(action: { /* How to do equivalent to popToRootViewController() here?? */ } )
{ Text("Pop two levels to Master View.") }
}
}
}
struct DetailViewA: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
VStack {
Text("This is Detail View A.")
NavigationLink(destination: DetailViewB() )
{ Text("Push to Detail View B.") }
Button(action: { self.presentationMode.value.dismiss() } )
{ Text("Pop one level to Master.") }
}
}
}
struct MasterView: View {
var body: some View {
VStack {
Text("This is Master View.")
NavigationLink(destination: DetailViewA() )
{ Text("Push to Detail View A.") }
}
}
}
struct ContentView: View {
var body: some View {
NavigationView {
MasterView()
}
}
}
推荐答案
在 NavigationLink
上将视图修饰符 isDetailLink
设置为 false
是让 pop-to-root 工作的关键.isDetailLink
默认为 true
并且适应包含视图.例如,在 iPad 横向上,拆分视图是分开的,并且 isDetailLink
确保目标视图将显示在右侧.因此,将 isDetailLink
设置为 false
意味着目标视图将始终被推送到导航堆栈上;因此可以随时弹出.
Setting the view modifier isDetailLink
to false
on a NavigationLink
is the key to getting pop-to-root to work. isDetailLink
is true
by default and is adaptive to the containing View. On iPad landscape for example, a Split view is separated and isDetailLink
ensures the destination view will be shown on the right-hand side. Setting isDetailLink
to false
consequently means that the destination view will always be pushed onto the navigation stack; thus can always be popped off.
在 NavigationLink
上将 isDetailLink
设置为 false
的同时,将 isActive
绑定传递到每个后续目标视图.最后当你想弹出到根视图时,将值设置为 false
它将自动弹出所有内容:
Along with setting isDetailLink
to false
on NavigationLink
, pass the isActive
binding to each subsequent destination view. At last when you want to pop to the root view, set the value to false
and it will automatically pop everything off:
import SwiftUI
struct ContentView: View {
@State var isActive : Bool = false
var body: some View {
NavigationView {
NavigationLink(
destination: ContentView2(rootIsActive: self.$isActive),
isActive: self.$isActive
) {
Text("Hello, World!")
}
.isDetailLink(false)
.navigationBarTitle("Root")
}
}
}
struct ContentView2: View {
@Binding var rootIsActive : Bool
var body: some View {
NavigationLink(destination: ContentView3(shouldPopToRootView: self.$rootIsActive)) {
Text("Hello, World #2!")
}
.isDetailLink(false)
.navigationBarTitle("Two")
}
}
struct ContentView3: View {
@Binding var shouldPopToRootView : Bool
var body: some View {
VStack {
Text("Hello, World #3!")
Button (action: { self.shouldPopToRootView = false } ){
Text("Pop to root")
}
}.navigationBarTitle("Three")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
这篇关于SwiftUI:如何弹出到根视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!