我有一个ContentView
,它是导航到DetailView.
的根视图。DetailView
有一个navbaritem
,可以导航到Help2
视图。 我要单击一个按钮以关闭Help2
视图并返回到DetailView
(Help2
来自的视图)。
当前,当我按下Help2
视图上的按钮时,它关闭了该视图,但是它将我返回到根ContentView
而不是DetailView
。如果我导航到Help2
视图,然后手动单击后退按钮导航到DetailView
,它将转到DetailView
。然后,如果我立即单击navbaritem
返回到Help2
视图,然后单击该按钮以关闭该视图,它将转到DetailView
而不是ContentView
如预期的那样。
ContentView :
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView()) {
Text("Show Detail View")
}.navigationBarTitle("Navigation")
}
}
}
}
DetailView :
struct DetailView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
VStack{
Button(action: {
self.presentationMode.wrappedValue.dismiss()
})
{
Text("Root")
}
}
.navigationBarTitle("DetailView", displayMode: .inline)
.navigationBarItems(trailing:
NavigationLink(destination: Help2()){
Image(systemName: "plus").imageScale(.medium)
}
)
}
}
帮助2 :
struct Help2: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
})
{
Text("DetailView")
}
}
}
答案 0 :(得分:1)
可能的解决方案可能是将.navigationBarItems
移到struct DetailView: View {
@Environment(\.presentationMode) var presentationMode
@State var isLinkActive = false
var body: some View {
VStack {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
})
{
Text("Root")
}
}
.background(
NavigationLink(destination: Help2(), isActive: $isLinkActive) {
EmptyView()
}
.hidden()
)
.navigationBarTitle("DetailView", displayMode: .inline)
.navigationBarItems(trailing:
Button(action: {
self.isLinkActive = true
}) {
Image(systemName: "plus").imageScale(.medium)
}
)
}
}
之外:
excelFile = tempfile.gettempdir() + "\\" + "Employees.xlsx"
wkb.save(excelFile)
subprocess.Popen(excelFile, shell=True)