我是 SwiftUI 框架的新手。我正在尝试实施
NavigationStack
。我想通过按钮操作进行导航,而不是使用 NavigationLink
。其背后的原因是,一旦在按钮操作上执行特定功能,我就需要导航。
struct AView: View {
@State private var actionss = [Int]()
var body: some View {
NavigationStack(path: $actionss) {
VStack {
Button("test") {
actionss.append(0)
}
}
.navigationDestination(for: Int.self) { _ in
BView()
}
}
}
}
上面的代码
AView
可以很好地导航BView
。唯一的问题是,如果不使用 CView
,我无法从 BView
导航到 NavigationLink
。
我还需要在从
BView
导航到 CView
之前执行特定功能。
假设工作已在
BView
上完成,您也可以使用 .navigationDestination
:
struct AView: View {
@State private var actionss = [Int]()
var body: some View {
NavigationStack(path:$actionss) {
VStack{
Button("show BView") {
actionss.append(0)
}
}
.navigationDestination(for: Int.self) { data in
BView()
}
.navigationTitle("AView")
}
}
}
struct BView: View {
@State var show: Bool = false
var body: some View {
VStack {
Button("show CView") {
show = true
}
}
.navigationDestination(isPresented: $show) {
CView()
}
.navigationTitle("BView")
}
}
struct CView: View {
var body: some View {
Text("Hello")
.navigationTitle("CView")
}
}