SwiftUI - Is there a popViewController equivalent in SwiftUI?

后端 未结 13 2073
说谎
说谎 2020-12-12 20:18

I was playing around with SwiftUI and want to be able to come back to the previous view when tapping a button, the same we use popViewController inside a

相关标签:
13条回答
  • 2020-12-12 20:59

    Use @Environment(\.presentationMode) var presentationMode to go back previous view. Check below code for more understanding.

    import SwiftUI
    
    struct ContentView: View {
    
    
        var body: some View {
    
            NavigationView {
                ZStack {
                    Color.gray.opacity(0.2)
    
                    NavigationLink(destination: NextView(), label: {Text("Go to Next View").font(.largeTitle)})
                }.navigationBarTitle(Text("This is Navigation"), displayMode: .large)
                    .edgesIgnoringSafeArea(.bottom)
            }
        }
    }
    
    struct NextView: View {
        @Environment(\.presentationMode) var presentationMode
        var body: some View {
            ZStack {
                Color.gray.opacity(0.2)
            }.navigationBarBackButtonHidden(true)
                .navigationBarItems(leading: Button(action: {
                    self.presentationMode.wrappedValue.dismiss()
                }, label: { Image(systemName: "arrow.left") }))
                .navigationBarTitle("", displayMode: .inline)
        }
    }
    
    
    struct NameRow: View {
        var name: String
        var body: some View {
            HStack {
                Image(systemName: "circle.fill").foregroundColor(Color.green)
                Text(name)
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    0 讨论(0)
  • 2020-12-12 21:01

    In the destination pass the view you want to redirect, and inside block pass data you to pass in another view.

    NavigationLink(destination: "Pass the particuter View") {
        Text("Push")
    }
    
    0 讨论(0)
  • 2020-12-12 21:02

    You can also do it with .sheet

    .navigationBarItems(trailing: Button(action: {
                self.presentingEditView.toggle()
            }) {
                Image(systemName: "square.and.pencil")
            }.sheet(isPresented: $presentingEditView) {
                EditItemView()
            })
    

    In my case I use it from a right navigation bar item, then you have to create the view (EditItemView() in my case) that you are going to display in that modal view.

    https://developer.apple.com/documentation/swiftui/view/sheet(ispresented:ondismiss:content:)

    0 讨论(0)
  • 2020-12-12 21:03

    It seems that a ton of basic navigation functionality is super buggy, which is disappointing and may be worth walking away from for now to save hours of frustration. For me, PresentationButton is the only one that works. TabbedView tabs don't work properly, and NavigationButton doesn't work for me at all. Sounds like YMMV if NavigationButton works for you.

    I'm hoping that they fix it at the same time they fix autocomplete, which would give us much better insight as to what is available to us. In the meantime, I'm reluctantly coding around it and keeping notes for when fixes come out. It sucks to have to figure out if we're doing something wrong or if it just doesn't work, but that's beta for you!

    0 讨论(0)
  • 2020-12-12 21:04

    instead of NavigationButton use Navigation DestinationLink

    but You should import Combine

    struct AView: View {
     var link: NavigationDestinationLink<BView>
    var publisher: AnyPublisher<Void, Never>
    
    init() {
        let publisher = PassthroughSubject<Void, Never>()
        self.link = NavigationDestinationLink(
            BView(onDismiss: { publisher.send() }),
            isDetail: false
        )
        self.publisher = publisher.eraseToAnyPublisher()
    }
    
    var body: some View {
        NavigationView {
            Button(action:{
            self.link.presented?.value = true
    
    
     }) {
                Text("Go to B")
            }.onReceive(publisher, perform: { _ in
                self.link.presented?.value = false
            })
        }
    }
    }
    
    struct BView: View {
    var onDismiss: () -> Void
    var body: some View {
        Button(action: self.onDismiss) {
            Text("Come back to A")
        }
    }
    }
    
    0 讨论(0)
  • 2020-12-12 21:05

    Below works for me in XCode11 GM

    self.myPresentationMode.wrappedValue.dismiss()
    
    0 讨论(0)
提交回复
热议问题