Skip to content

Instantly share code, notes, and snippets.

@takoikatakotako
Created March 24, 2021 08:40
Show Gist options
  • Save takoikatakotako/d5a2781b0fc46a0a60feebcfc5235e23 to your computer and use it in GitHub Desktop.
Save takoikatakotako/d5a2781b0fc46a0a60feebcfc5235e23 to your computer and use it in GitHub Desktop.
SwiftUIで初めの画面に遷移する(popToRootViewController)
import SwiftUI
struct ContentView: View {
@State var isActive : Bool = false
var body: some View {
NavigationView {
NavigationLink(
destination: SecondView(rootIsActive: $isActive),
isActive: $isActive
) {
Text("Go SecondView")
}
.isDetailLink(false)
.navigationBarTitle("Root")
}
}
}
struct SecondView: View {
@Binding var rootIsActive : Bool
var body: some View {
NavigationLink(destination: ThirdView(shouldPopToRootView: self.$rootIsActive)) {
Text("Go ThirdView")
}
.isDetailLink(false)
.navigationBarTitle("SecondView")
}
}
struct ThirdView: View {
@Binding var shouldPopToRootView : Bool
var body: some View {
VStack {
Button (action: { shouldPopToRootView = false } ){
Text("Pop to RootView")
}
}.navigationBarTitle("ThirdView")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@takoikatakotako
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment