SwiftUI NavigationLink Dynamic Destination

Hi,

Is there a way of specifying a destination for a NavigationLink that is passed as init parameter?

I have this code:
Code Block Swiftstruct RowLine: View {    var text: String    var imageName: String    var body: some View {        NavigationLink(destination: text == "Settings" ? SettingsView() : MyInfoView()){        				HStack{            Image(systemName: imageName)            Text(text)        }        }    }}

And my intention is to have, instead of the ternary operator in the destination parameter, directly the view that I passed as parameter at init.

For example, call the view like this:

Code Block SwiftRowLine(text: "Settings", imageName: "gearshape", nextView: myNextView())


And the view remains like this:

Code Block Swiftstruct RowLine: View {    var text: String    var imageName: String    var nextView: View /*Of course, this is ilegal*/    var body: some View {        NavigationLink(destination: nextView){        HStack{            Image(systemName: imageName)            Text(text)        }        }    }}


With this approach, I can have multiple items in a list with different destination but with the same codebase.

Thank you so much.
F.


Answered by OOPer in 618828022
Please try generic.
Code Block struct RowLine<TargetView: View>: View {    var text: String    var imageName: String    var nextView: TargetView    var body: some View {        NavigationLink(destination: nextView) {            HStack {                Image(systemName: imageName)                Text(text)            }        }    }}



Accepted Answer
Please try generic.
Code Block struct RowLine<TargetView: View>: View {    var text: String    var imageName: String    var nextView: TargetView    var body: some View {        NavigationLink(destination: nextView) {            HStack {                Image(systemName: imageName)                Text(text)            }        }    }}



SwiftUI NavigationLink Dynamic Destination
 
 
Q