简体   繁体   English

SwiftUI NavigationLink pop

[英]SwiftUI NavigationLink pop

I have navigated to a new view with NavigationLink and I want to pop back to where I was programatically.我已经使用NavigationLink导航到了一个新视图,并且我想以编程方式返回到我所在的位置。 Is it yet possible in swiftUI? swiftUI 中还有可能吗? I know for the modal presentation we could use the .isPresented environment value but how about navigation?我知道对于模态演示,我们可以使用.isPresented环境值,但是导航呢?

You can really simply create custom back button.您真的可以简单地创建自定义后退按钮。 Only two lines code 🔥只有两行代码🔥

@Environment(\.presentationMode) var presentationMode
self.presentationMode.wrappedValue.dismiss()

Example:例子:

import SwiftUI

struct FirstView: View {
    @State var showSecondView = false
    
    var body: some View {
        NavigationLink(destination: SecondView(),isActive : self.$showSecondView){
            Text("Push to Second View")
        }
    }
}


struct SecondView : View{
    @Environment(\.presentationMode) var presentationMode

    var body : some View {    
        Button(action:{ self.presentationMode.wrappedValue.dismiss() }){
            Text("Go Back")    
        }    
    }
}

Yes you can now programmatically pop a NavigationLink View using the following code:是的,您现在可以使用以下代码以编程方式弹出 NavigationLink 视图:

import SwiftUI

struct MainViewer : View{
    @State var showView = false
    var body : some View {

        NavigationLink(destination:DestView(showView: self.$showView),isActive : self.$showView){
       Text("Push View")

    }
   }
}


struct DestView : View{
    @Binding var showView : Bool
    var body : some View {

        Button(action:{self.showView = false}){
            Text("Pop Screen")

    }

   }
}

This has to be a bug currently.这必须是当前的错误。 Apple provides the boilerplate code to allow the "Back" or 'pop' functionality built in to a navigation view 'DetailView'. Apple 提供样板代码以允许内置于导航视图“DetailView”中的“返回”或“弹出”功能。 My only guess is Apple is working out the kinks in fully implementing Combine within SwiftUI in the backend to implement 'push' and 'pop' type of actions.我唯一的猜测是 Apple 正在解决在后端的 SwiftUI 中完全实现 Combine 的问题,以实现“推送”和“弹出”类型的操作。 I can't imagine SwiftUI coming out of beta without this functionality more accessible than creating a Combine publisher to update state similar to what RyanAshcraft did above.我无法想象 SwiftUI 在没有此功能的情况下推出测试版,比创建一个 Combine 发布者来更新类似于上面 RyanAshcraft 所做的状态更容易访问。

If you use @Environment(.dismiss) var dismiss如果你使用 @Environment(.dismiss) var dismiss

in the view that is your destination, then you can call dismiss()在您的目的地视图中,您可以调用dismiss()

where you want from within that view.在该视图中您想要的位置。

Example:例子:

import SwiftUI

struct MainView: View {

var body: some View {
    NavigationLink(destination: SecondView()){
            Text("Push to Destination View")
        }
    }
}


struct DestinationView : View{
    @Environment(\.dismiss)

     var body : some View {    
        Button(action:{ self.dismiss() }){
             Text("Go Back")    
         }    
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM