1

I want to navigate to HelloWorldView when "Hello" is clicked. However, if the text "Bye" is clicked, then I want to navigate to the ByeWorldView. In the past, I was able to use isActive on the multiple NavigationLinks to get this to work, but Apple has deprecated this in iOS 16.2, so now I am back to the drawing board.

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationStack {
            ListView()
        }
    }
}

struct ListView: View {
    var body: some View {
        List {
            RowView()
            RowView()
            RowView()
        }
    }
}

struct RowView: View {
    @State var text: String = ""
    var body: some View {
        HStack{
            Button("Bye") {
                text = "bye"
            }
            Button("Hello") {
                text = "hello"
            }
            NavigationLink("", value: text).opacity(0)
        }
        .navigationDestination(for: String.self) { string in
            if(string == "hello") {
                HelloWorldView()
            } else {
                ByeWorldView()
            }
        }
    }
}

struct HelloWorldView: View {
    var body: some View {
        Text("Hello, World!")
    }
}

struct ByeWorldView: View {
    var body: some View {
        Text("Bye, World!")
    }
}

Screen capture of behavior

jh95
  • 379
  • 2
  • 12
  • You can bind the path with your `NavigationStack`. Check this for more details https://stackoverflow.com/questions/73723771/navigationstack-not-affected-by-environmentobject-changes/73726456#73726456 – Nirav D Jan 11 '23 at 02:55

0 Answers0