我的应用程序支持iOS 15.0,所以,我不能使用NavigationPath
。所以,我添加了NavigationView
并使用NavigationLink
进行导航。这是我的简单应用程序流程。
FirstView -〉SecondView -〉ThirdView -〉ForthView
我想从ForthView导航到SecondView。但是导航回来不起作用。下面是我的代码。
struct FirstView: View {
@State private var isActive: Bool = false
var body: some View {
VStack(spacing: 8) {
Label("Hello, world!", systemImage: "globe")
NavigationLink(destination: SecondView(), isActive: $isActive) {
Button {
isActive = true
} label: {
Text("Second")
}
}
}
.padding()
}
}
struct SecondView: View {
@State private var isActive: Bool = false
var body: some View {
VStack(spacing: 8) {
Text("Hello, World!")
NavigationLink(destination: ThirdView(shouldGoToSecond: $isActive), isActive: $isActive) {
Button {
isActive = true
} label: {
Text("Third")
}
}
}
.navigationTitle("Second")
}
}
struct ThirdView: View {
@Binding var shouldGoToSecond: Bool
@State private var isActive: Bool = false
var body: some View {
VStack {
Text("Hello, World!")
NavigationLink(destination: ForthView(shouldGoToSecond: $shouldGoToSecond), isActive: $isActive) {
Button {
isActive = true
} label: {
Text("Go to Forth")
}
}
}
.navigationTitle("Third")
}
}
struct ForthView: View {
@Binding var shouldGoToSecond: Bool
var body: some View {
VStack {
Text("Hello, World")
Button {
shouldGoToSecond = false
} label: {
Text("Go to Second")
}
}
}
}
我正在使用@State
变量来激活导航链接,并通过@Binding
将其传递到下一个视图,但它不起作用。
2条答案
按热度按时间bqucvtff1#
当您有多个导航时,您可以通过使用
NavigationLink
的isDetailLink
属性来实现它。enyaitl32#
你应该用
@Environment(\.presentationMode) var presentaionMode