var body: some View {
VStack {
List {
ForEach(starSigns) {ZodiacSign in
NavigationLink(destination: ZodiacSign.DestinationView) {
Text(ZodiacSign.Symbol)
Text(ZodiacSign.name)
}
}
}
}
}
}
I have this basic view which gets information from a list I created, but how do I get the destination view to work where an example of it would be AriesView(), I would like the destination to be there. Currently it comes up with an error of "Protocol 'View ' cannot conform to the protocol itself" Sorry for the complexity of this question, I was struggling to explain it.
THANK YOU IN ADVANCE!!!
I would do it this way with ViewBuilder:
struct ZodiacSign: Identifiable {
let id = UUID()
var name: String
var symbol: String // as per Swift conventions, should start with lowercase
// Replaced by ViewBuilder var DestinationView: View
}
@ViewBuilder func zodiacDestination(name: String) -> some View {
switch name {
case "Aries" : AriesView()
case "Taurus" : TaurusView()
case "Gemini" : GeminiView()
case "Cancer" : CancerView()
case "Leo" : LeoView()
case "Virgo" : VirgoView()
case "Libra" : LibraView()
case "Scorpio" : ScorpioView()
case "Sagittarius": SagittariusView()
case "Capricorn": CapricornView()
case "Aquarius" : AquariusView()
case "Pisces" : PiscesView()
default: EmptyView()
}
}
struct ContentView: View {
@State var starSigns = [ // Need to be a State var
ZodiacSign(name: "Aries", symbol: "♈︎"), // DestinationView: AriesView()),
ZodiacSign(name: "Taurus", symbol: "♉︎"), // DestinationView: TaurusView()),
ZodiacSign(name: "Gemini", symbol: "♊︎"), // DestinationView: GeminiView()),
ZodiacSign(name: "Cancer", symbol: "♋︎"), // DestinationView: CancerView()),
ZodiacSign(name: "Leo", symbol: "♌︎"), // DestinationView: LeoView()),
ZodiacSign(name: "Virgo", symbol: "♍︎"), // DestinationView: VirgoView()),
ZodiacSign(name: "Libra", symbol: "♎︎"), // DestinationView: LibraView()),
ZodiacSign(name: "Scorpio", symbol: "♏︎"), // DestinationView: ScorpioView()),
ZodiacSign(name: "Sagittarius", symbol: "♐︎"), // DestinationView: SagittariusView()),
ZodiacSign(name: "Capricorn", symbol: "♑︎"), // DestinationView: CapricornView()),
ZodiacSign(name: "Aquarius", symbol: "♒︎"), // DestinationView: AquariusView()),
ZodiacSign(name: "Pisces", symbol: "♓︎") // DestinationView: PiscesView())
]
var body: some View {
// VStack {
NavigationView { // You have to put in a NavigationView
List {
ForEach(starSigns, id: \.id) {zodiacSign in // should start with lowercase
NavigationLink(destination: zodiacDestination(name: zodiacSign.name)) {
Text(zodiacSign.symbol)
Text(zodiacSign.name)
}
}
}
}
}
}
struct AriesView: View {
var body: some View {
VStack {
Text("♈︎ Aries is a constellation…")
}
}
}
struct TaurusView: View {
var body: some View {
VStack {
Text("♉︎ Taurus is a constellation…")
}
}
}
struct GeminiView: View {
var body: some View {
VStack {
Text("♊︎ Gemini is a constellation…")
}
}
}