SwiftUI:弹出或关闭视图控制器

时间:2019-06-27 14:57:20

标签: swiftui

我正在研究SwiftUI,我写这篇文章来展示新的观点。

NavigationButton(destination: LanguagePage()) {
                            VStack(alignment: .leading) {
                                Text("Hello World")
                            }
                        }

在该LanguagePage中,当用户点击按钮(例如dismissviewcontroller或popviewcontroller)时,如何返回上一页?

2 个答案:

答案 0 :(得分:2)

不能。自Xcode 11 beta 2起,SwiftUI中没有手动导航堆栈管理功能。

答案 1 :(得分:0)

代替NavigationButton使用导航DestinationLink

但是您应该导入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")
}
}
}