0

I am building a macOS SwiftUI app in which I need to perform navigation from my View A to View B. Here is a small screenshot.

enter image description here

Here is my implementation:

struct ViewA: View {
    
    var body: some View {
        
        VStack {
            Text("VIEW A")
            
            Button("Go to View B") {
                // I want to go to View B and also pass some data to View B
            }
        }
    }
}

struct ViewB: View {
    
    let data: String
    
    var body: some View {
        VStack {
            Text("VIEW B")
        }
        
    }
}


struct ContentView: View {
    
    @EnvironmentObject var appState: AppState
    
    var body: some View {
        NavigationView {
            SideBar()
            ViewA()
        }
    }
}

In iOS I would use NavigationLink, but for macOS apps NavigationLink is not working as expected. Any ideas?

Mary Doe
  • 1,073
  • 1
  • 6
  • 22
  • 1
    macOS doesn't have the same stack-type navigation paradigm that iOS does. You'd have to do something like conditional rendering, ie `if showA { ViewA() } if showB { ViewB }`. *Or*, if `ViewB` corresponds to a different item in the sidebar, you could do programatic navigation with a `NavigationLink` in the sidebar with an `isActive` property that gets set to `true` when the button is pressed. – jnpdx Jan 16 '22 at 23:43
  • Thanks! I got it working through your advice. – Mary Doe Jan 17 '22 at 03:50
  • 1
    This should be helpful https://stackoverflow.com/a/61424783/12299030? – Asperi Jan 17 '22 at 06:00

0 Answers0