I have a menu which I display using SwiftUI List
. An example on the image below:
What I want to do is to use NavigationLink with different destination views. I tried to use switch
statement in the List block but Xcode throws an error:
Closure containing control flow statement cannot be used with function builder 'ViewBuilder'
Here is my code below:
struct HomeView: View {
enum MenuItem: String, CaseIterable, Identifiable {
var id : MenuItem {
self
}
case firstCase = "Staff"
case secondCase = "Projects"
case thirdCase = "Invoices"
}
var body: some View {
NavigationView {
List(MenuItem.allCases) { itemText in
switch itemText {
case .firstCase:
NavigationLink(destination: StaffDetail()) {
HomeMenuRow(itemText: itemText)
}
break
case .secondCase:
NavigationLink(destination: ProjectsDetail()) {
HomeMenuRow(itemText: itemText)
}
break
case .thirdCase:
NavigationLink(destination: InvoicesDetail()) {
HomeMenuRow(itemText: itemText)
}
break
}
}
.navigationBarTitle(Text("Menu"))
}
}
}
Looks like find some solution here but not sure how to use it in the List
object.