我正在研究SwiftUI,我写这篇文章来展示新的观点。
NavigationButton(destination: LanguagePage()) {
VStack(alignment: .leading) {
Text("Hello World")
}
}
在该LanguagePage中,当用户点击按钮(例如dismissviewcontroller或popviewcontroller)时,如何返回上一页?
答案 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")
}
}
}