SwiftUI - Is there a popViewController equivalent in SwiftUI?

后端 未结 13 2131
说谎
说谎 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 21:04

    instead of NavigationButton use Navigation DestinationLink

    but You should import Combine

    struct AView: View {
     var link: NavigationDestinationLink
    var publisher: AnyPublisher
    
    init() {
        let publisher = PassthroughSubject()
        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")
        }
    }
    }
    

提交回复
热议问题