swiftUI多层navigationLink跳转后直接回到第一层
程序员文章站
2024-03-24 12:41:58
...
- Xcode 12.5.2
- iOS 14
为*上的一个答案,链接找不到了
//
// TestView.swift
// Marking (iOS)
//
// Created by jc_xcode on 2021/7/24.
//
import SwiftUI
import SwiftUI
struct ContentViewMain: View {
@State var isActive : Bool = false
var body: some View {
NavigationView {
NavigationLink(
destination: ContentView2(rootIsActive: self.$isActive),
isActive: self.$isActive
) {
Text("Hello, World!")
}
.isDetailLink(false)
.navigationBarTitle("Root")
}
}
}
struct ContentView2: View {
@Binding var rootIsActive : Bool
var body: some View {
NavigationLink(destination: ContentView3(shouldPopToRootView: self.$rootIsActive)) {
Text("Hello, World #2!")
}
.isDetailLink(false)
.navigationBarTitle("Two")
}
}
struct ContentView3: View {
@Binding var shouldPopToRootView : Bool
var body: some View {
VStack {
Text("Hello, World #3!")
Button (action: { self.shouldPopToRootView = false } ){
Text("Pop to root")
}
}.navigationBarTitle("Three")
}
}
struct ContentViewMain_Previews: PreviewProvider {
static var previews: some View {
ContentViewMain()
}
}