I'm discovering SwiftUI, thinking that it is the future for developping Applications.
the first thing (I'm sure it is very simple) I have difficulties is making a choice in a dynamic List.
In a classic Swift I make it like this
In my menuViewController, I have these declarations:
private var choix = ["Adjectifs", "Adverbes", "Noms", "Verbes", "Prépositions", "Expressions", "Phrases", "Racines", "Quizz"]//, "test"]
private var actions = [adjectifsViewController.self, adverbesViewController.self, nomsViewController.self, verbesViewController.self, prepositionsViewController.self, expressionsViewController.self, phrasesViewController.self, associationsViewController.self, listerQuizzViewController.self] //, menu1ViewController.self]
I draw a UITableView with choices ad when I click on a row of my tableView:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if actions[indexPath.row] != "" {
let aClass = actions[indexPath.row]
let controller = aClass.loadFromNib()
self.show(controller, sender: self)
}
}
Now of course I want to write this in SwiftUI
first I have a struct like this:
struct IvritMenu: Identifiable {
let id: Int
let text: String
let lien: click
}
enum click: Hashable {
case adjectifs
case adverbes
case noms
case verbes
}
then in the View, I write the variable:
var menu = [
IvritMenu(id: 0, text: "Adjectifs", lien: .adjectifs),
IvritMenu(id: 1, text: "Adverbes", lien: .adverbes),
IvritMenu(id:2, text: "Noms", lien: .noms),
IvritMenu(id:3, text: "Verbes", lien: .verbes)]
and Finally:
var body: some View {
NavigationView {
List(menu) { choix in
ListeChoix(choix: choix)
}
.navigationBarTitle(Text("Ivrit"))
}
}
now the difficult part (for me) is to write ListeChoix
struct ListeChoix: View {
var choix: IvritMenu
var body: some View {
switch choix.lien {
case .adjectifs:
return Adjectifs()
defaul make itt:
return Adverbes()
}
}
}
this generate an error Function declares an opaque return type, but the return statements in its body do not have matching underlying types
I think this error is caused by the return which returns different types of View, but I don't know how to make it
The change
struct IvritMenu2: Identifiable {
let id: Int
let text: String
let destination: AnyView
}
Seems to work properly.
I am not sure of the answer to your question.
does it means that the view Adjectifs and Adverbes are always created,
But, I think that they are created when you navigate the link.
If OK, don't forget to close the thread.