MacOS + SwiftUI + Xcode: Dismiss view with a button

Hello,

I have a main view from which I call a second view based on an on tap gesture.
This works fine. Once I get to the second view I would like to have a button that will dismiss this view and return me to the previous view. I have looked at numerous posts on the net but they appear to only apply to sheets.
Any input on what to try would be appreciated. Below is the code for my main view and the second view.

Chris

Main View:

import SwiftUI

struct testView: View {

@ObservedObject var viewModel : testViewModel  // no () to view model to be passed in.  view now has access to view model functions
@State var cellId : Int = 0
@State var navLinkActivated : Bool = false

var columns = [
    GridItem(.fixed(100), spacing: 0.1),
    GridItem(.fixed(100), spacing: 0.1),
]

var body: some View {

    let csvData = viewModel.data

        NavigationView {

            NavigationLink("Select", destination: testDataEntry(cellId: cellId), isActive: $navLinkActivated)


        VStack {
            LazyVGrid(columns: columns, alignment: .center, spacing: 0) {
                    Rectangle()
                        .border(Color.gray)
                        .foregroundColor(.white)
                        .frame(width: 100, height: 30)
                        .overlay(Text (csvData[0].Date))
                    Rectangle()
                        .border(Color.gray)
                        .foregroundColor(.white)
                        .frame(width: 100, height: 30)
                        .overlay(Text (csvData[0].Value))
            }.padding(.bottom, -7.8) // end of first lazy v grid
            ScrollView {
                LazyVGrid(columns: columns, alignment: .center, spacing: 0) {
                    ForEach (1..<csvData.count){row in
                        Rectangle()
                            .border(Color.gray)
                            .foregroundColor(.white)
                            .frame(width: 100, height: 30)
                            .overlay(Text (csvData[row].Date))
                        Rectangle()
                            .border(Color.gray)
                            .foregroundColor(.white)
                            .frame(width: 100, height: 30)
                            .overlay(Text (csvData[row].Value))
                            .onTapGesture {
                                print("\(csvData[row].id)")
                                cellId = csvData[row].id
                                navLinkActivated = true
                            }
                    } // end of for each
                } // endo of second lazy v grid
            } // end of scroll view
        } // end of vstack
    } // end of navigation view
} // end of body var

}

Second View:

import SwiftUI

struct testDataEntry: View {

let cellId : Int
var body: some View {
    Text("\(cellId)")
}

}

Please format your code so that every line is inside a code fence, and use proper indentation.

This can easily be solved using a Binding. Here's an example:

struct ContentView: View {

    @State private var childViewIsPresented = false

    var body: some View {
        NavigationView {
            NavigationLink(
                "Select",
                destination: ChildView(isPresented: $childViewIsPresented),
                isActive: $childViewIsPresented
            )
        }
    }    
}

struct ChildView: View {
    
    @Binding var isPresented: Bool

    var body: some View {
        VStack {
            Text("ChildView")
            Button("Dismiss") {
                self.isPresented = false
            }
        }   
    }
}

Just implemented your suggestion. Works great.
Thanks Peter.

Chris

Terms of Service

Privacy Policy

Cookie Policy