我对Swift还是很陌生,可能是我要解决所有这些错误,但是我有一个无法解决的错误-在NavigationView中,View1中的NavigationLink打开到View 2,然后视图具有到View3的NavigationLink。 View3显示了View1中的标题,这显然是错误的,当按下后退按钮时,它还会返回到View1,而不是View2。我使用这些链接是否错误,还是SwiftUI问题?我在下面做的事情的简化示例。
struct FirstView: View {
var body: some View {
Text("Hello, World #1!")
.navigationBarItems(trailing:
NavigationLink(destination: SecondView()){
Image(systemName: "folder.badge.plus")
}
)
}
}
struct SecondView: View {
var body: some View {
Text("Hello, World #2!")
.navigationBarItems(trailing:
NavigationLink(destination: ThirdView()){
Image(systemName: "folder.badge.plus")
}
)
}
}
struct ThirdView: View {
var body: some View {
Text("Hello, World #3!")
}
}
答案 0 :(得分:0)
有趣的是,我在导航跳过一个视图时遇到了同样的问题。不过你的问题是不同的。第一个视图只会声明 NavigationView
。之后,您只需使用导航链接。
重构:
import SwiftUI
struct FirstView: View {
var body: some View {
NavigationView {
VStack {
Text("Hello, World #1!")
.padding()
NavigationLink(destination: SecondView()){
Image(systemName: "folder.badge.plus")
}
}
// a navigationBarTitle modifier inside!
.navigationBarTitle(Text("Hello"))
}
}
}
struct SecondView: View {
var body: some View {
VStack {
Text("Hello, World #2!")
.padding()
NavigationLink(destination: ThirdView()){
Image(systemName: "folder.badge.plus")
}
}
}
}
struct ThirdView: View {
var body: some View {
Text("Hello, World #3!")
}
}
struct FirstView_Previews: PreviewProvider {
static var previews: some View {
FirstView()
}
}