0

I would like to go to a new view from inside my modal.

Currently I have my default view. I then present my modal. In that modal I can close it but I also have a another button that I would like to take me to another view. (NewView)

IS this possible?

Default view:

import SwiftUI

struct ContentView: View {


    @State private var showModal = false

    var body: some View {

       Button("Show Modal") {

          self.showModal.toggle()
       }.sheet(isPresented: $showModal) {
            ModalView(showModal: self.$showModal)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Modal View:

struct ModalView: View {

    @Binding var showModal: Bool

    var body: some View {

        VStack (spacing: 30){

            Text("My Modal View")
                .font(.title)
                .padding()



            Button("Close modal") {
                self.showModal.toggle()
            }


            Button("Close modal and go to a new view") {



            }

        }
    }
}

NewView:

struct NewView: View {

    var body: some View {

       Text("My New View")
    }
}
user1631763
  • 245
  • 1
  • 3
  • 8

1 Answers1

1

I understood your question in 2 different ways, and luckily I have both solutions.

If you want the following flow: ViewA -> Opens ViewB As modal -> opens ViewC as NavigatedView in ViewB

Then all you need to change is your ModalView to this

struct ModalView: View {

    @Binding var showModal: Bool

    var body: some View {
        NavigationView {
            VStack (spacing: 30){
                NavigationLink(destination: NewView()) {
                    Text("Take me to NewView")
                }
                Text("My Modal View")
                    .font(.title)
                    .padding()



                Button("Close modal") {
                    self.showModal.toggle()
                }


                Button("Close modal and go to a new view") {



                }

            }
        }
    }
}

IF however what you mean is ViewA -> opens ViewsB as Moda -> ViewB then tells ViewA to navigate to ViewC

then please refer to this link, someone asked this question earlier and I provided the solution

SwiftUI transition from modal sheet to regular view with Navigation Link

Muhand Jumah
  • 1,726
  • 1
  • 11
  • 26