Pop 和 Push 的导航在 SwiftUI 中不起作用?



我有两个屏幕。第一个屏幕有以下代码

NavigationLink(destination: SecondScreen()) {
Text("Scan Qr Code")
}

NavigationLink导航到按预期工作的第二个屏幕。在第二个屏幕中

struct SecondScreen: View {
@Environment(.presentationMode) var presentation
var body: some View{
Button("Done") {
self.presentation.wrappedValue.dismiss()
}
}}

第二个屏幕中的按钮弹出到第一个屏幕,该屏幕按预期工作,但是一旦我返回第一个屏幕并单击NavigationLink,它就不会移动到第二个屏幕。

这是一个解决方案。使用第一个屏幕上的按钮而不是导航链接,并使用该按钮弹出工作表。我测试了这个,它对我有用!

struct ContentView: View {
@State private var showingAddExpense = false
var body: some View {
NavigationView{
Button(action: {
self.showingAddExpense = true
}) {
Text("Button")
}
.sheet(isPresented: $showingAddExpense) {
SecondScreen()
}
}
}
}    

struct SecondScreen: View {
@Environment(.presentationMode) var presentation
var body: some View{
NavigationView{
Button("Done") {
self.presentation.wrappedValue.dismiss()
}
}
}
}

最新更新