New to SwiftUI and I cannot find a way to pass multiple views in my model which populates the List in the NavigationStack .
(Below I have placed all my code into one file to make it easier to examine)
I populate List from MainMenuFramework , which would hold the title, graphic, and eventually a subMenu to go to for each List element. i.e. SubView1, SubView2, and so on.
When within the MainMenuFramework I try to add let subMenu : View
I first am told to add it as any View
. But then I have to make MainMenuFramework conform to the Equatable protocol. And from there I am told that MainMenuFramework has to conform to Hashable, even though it is.
The real problem is that I am falling deeper into the well and am too new to understand what is happening. Is there perhaps a way to pass a pointer to the subviews?
I don’t just want the answer, I’d like to understand the process as well.
Thank you.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationStack {
ZStack {
List(MainMenuData.mainMenuFramework) { menuItem in
NavigationLink {
// menuItem.subMenu
} label: {
ListCell(framework: menuItem)
}
}
.navigationTitle("Main Menu")
}
}
}
}
struct ListCell: View {
let framework : MainMenuFramework
var body: some View {
HStack {
VStack(alignment: .center){
Image(systemName: framework.imageName)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 50, height: 50, alignment: .center)
}
Text(framework.description)
.font(.title)
.padding(.leading, 30)
}
.listRowInsets(.init(top: 10,
leading: 10,
bottom: 10,
trailing: 10))
}
}
struct FrameworkTitleView: View{
let framework : MainMenuFramework
var body: some View {
VStack {
Image(systemName: framework.imageName)
.resizable()
.frame(width: 75, height: 75)
.padding(.top, 20)
.padding(.bottom, 5)
Text(framework.name)
.font(.title2)
.fontWeight(.semibold)
.foregroundColor(Color(.label))
.scaledToFit()
.minimumScaleFactor(0.6)
}
.padding()
}
}
struct MainMenuFramework: Hashable, Identifiable {
let id : Int
let name : String
let imageName : String
let urlString : String
let description : String
}
struct MainMenuData {
static let mainMenuFramework = [
MainMenuFramework(
id: 001,
name: "Setup",
imageName: "list.clipboard",
urlString: "",
description: "Setup"),
MainMenuFramework(
id: 002,
name: "Account",
imageName: "person.circle",
urlString: "",
description: "Account Settings"),
MainMenuFramework(
id: 003,
name: "Help",
imageName: "questionmark.circle",
urlString: "",
description: "Support")
]
}
struct DetailView: View {
var body: some View {
Text("This is the detail view 1")
}
}
struct DetailView2: View {
var body: some View {
Text("This is the detail view 2")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}