Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

ios - SwiftUI - How can I dismiss a view to root and then push a second view immediately afterwards?

Couldn't find anything relating to this issue in SwiftUI.

I have three views currently, RootView, DetailView1 and DetailView2. RootView will feature a button to push and show DetailView1, inside DetailView1 there will be a NavigationLink to dismiss DetailView1 to RootView and push DetailView2.

struct DetailView1: View {
    @Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
    var body: some View {
        VStack {
            NavigationLink(destination: DetailView2()) {
                Text("Tap to dismiss DetailView and show DetailView2")
                    .onTapGesture {
                        self.presentationMode.wrappedValue.dismiss()
                    }
            }
        }
    }
}

struct DetailView2: View {
    @Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
    var body: some View {
        Button(
            "This is DetailView2",
            action: { self.presentationMode.wrappedValue.dismiss() }
        )
    }
}

struct RootView: View {
    var body: some View {
        VStack {
            NavigationLink(destination: DetailView1())
            { Text("This is root view. Tap to go to DetailView") }
        }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            RootView()
        }
    }
}

Expected behaviour: User presses NavigationLink in DetailView1, the view is dismissed to RootView and DetailView2 is pushed up.

Current behaviour: User presses NavigationLink in DetailView1, the view is dismissed to RootView, DetailView2 is not pushed.

question from:https://stackoverflow.com/questions/65857479/swiftui-how-can-i-dismiss-a-view-to-root-and-then-push-a-second-view-immediate

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this by passing the active state of the second view. And change the @State value. It's a similar concept to the completion block in UIKit.

DetailView1

struct DetailView1: View {
    @Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
    
    @Binding var isActiveDetails2: Bool //<-- Here

    var body: some View {
        VStack {
            Button("Tap to dismiss DetailView and show DetailView2") {
                isActiveDetails2 = true //<-- Here
            }
        }
    }
}

RootView

struct RootView: View {
    @Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
    @State private var isActiveDetails2: Bool = false
    
    var body: some View {
        VStack {
            NavigationLink(destination: DetailView1(isActiveDetails2: $isActiveDetails2))
            { Text("This is root view. Tap to go to DetailView") }
            
            
            NavigationLink(destination: DetailView2(), isActive: $isActiveDetails2) { //<-- Here
                EmptyView()
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...