UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to refer to a View

Forums > SwiftUI

My app has a central list of choices, each of which takes the user to a different screen; the destinations have almost nothing in common. I've coded the main screen like this:

struct Tab: Hashable {
    let title: String
    let desc: String
  let destination: ????????
}

    @State private var tabs: [Tab] =
        [Tab(title: "Ogden", desc: "Life, earnings and pension"),
         Tab(title: "Term certain", desc: "Single term certain and Swift"),
         Tab(title: "Multiple term certain", desc: "Term certain over multiple periods"),
         Tab(title: "Periodic", desc: "Recurring at fixed intervals"),
         Tab(title: "Fatal accidents", desc: "Table E and F discount factors\nFinancial dependency"),
         Tab(title: "Interest", desc: "Cumulative special damages interest"),
         Tab(title: "RPI", desc: "Retail prices index adjustments"),
         Tab(title: "Care", desc: "Care and assistance calculations")]

    var body: some View {
        NavigationView {
            Form {
                List (tabs.indices, id: \.self) { index in
                    NavigationLink(destination: xxxx) {
                        VStack (alignment: .leading) {
                            Text(tabs[index].title).font(.headline).bold()
                            Text(tabs[index].desc).font(.callout)
                        }
                    }
                }
            }.navigationTitle("PI Calculator")
        }

That is, a series of titles with a short description underneath. It looks more or less as I want it (I can smarten up the appearance later, and the headings and descriptions may be kept in a file and loaded at runtime).

The sensible thing would seem to be to put some kind of reference to the destination view in the Tab structure, so the xxxx in the NavigationLink would be replaced with tab.destination.

How do I do that? What is the "type" of a View?

Or am I on completely the wrong track?

Any suggestions gratefully received.

Jeremy

2      

Hi Jeremy,

Question is the Views you are going to different or is it the same custom view with different information on it?

However have got it work but it probably not the best way to do it.

enum SelectedView {
    case one, two, three
}
struct Tab: Identifiable {
    let id = UUID()
    let title: String
    let desc: String
    let destination: SelectedView
}
struct ContentView: View {
    var tabs: [Tab] = [
        Tab(title: "View One", desc: "Details of View One", destination: .one),
        Tab(title: "View Two", desc: "Details of View Two", destination: .two),
        Tab(title: "View Three", desc: "Details of View Three", destination: .three)
    ]
    var body: some View {
        NavigationView {
            List {
                ForEach(tabs) { tab in
                    NavigationLink(
                        destination: pickedView(tab.destination),
                        label: {
                            VStack(alignment: .leading) {
                              Text(tab.title)
                                  .font(.headline.bold())
                              Text(tab.desc)
                                  .font(.callout)
                            }

                        })
                }
            }
            .navigationTitle("Title")
        }
    }

    func pickedView(_ destination: SelectedView) -> some View {
        Group {
            switch destination {
            case .one:
                ViewOne() // custom views. I only put Text in to see if worked
            case .two:
                ViewTwo()
            case .three:
                ViewThree()
            }
        }
    }
}

2      

Nigel, they're completely different views which have either a little or nothing in common.

Since I posted, I found a solution which is very similar to yours - but I think yours is better. Thanks.

Jeremy

2      

Hi Jeremy

Have found another way but not sure if very good but works

struct Tab: Identifiable {
    let id = UUID()
    let title: String
    let desc: String
    let destination: AnyView
}
struct ContentView: View {
    var tabs: [Tab] = [
        Tab(title: "View One", desc: "Details of View One", destination: AnyView(ViewOne())),
        Tab(title: "View Two", desc: "Details of View Two", destination: AnyView(ViewTwo())),
        Tab(title: "View Three", desc: "Details of View Three", destination: AnyView(ViewThree()))
    ]
    var body: some View {
        NavigationView {
            List {
                ForEach(tabs) { tab in
                    NavigationLink(
                        destination: tab.destination,
                        label: {
                            VStack(alignment: .leading) {
                              Text(tab.title)
                                  .font(.headline.bold())
                              Text(tab.desc)
                                  .font(.callout)
                            }
                        })
                }
            }
            .navigationTitle("Title")
        }
    }
}

2      

Since enums can conform to View, you can also tweak Nigel's first solution so that you don't have to use AnyView or a separate function to pick the View (wrapped in a Group):

  import SwiftUI

enum SelectedView: View {
    case one, two, three

    //use @ViewBuilder so we won't have to wrap in AnyView
    @ViewBuilder
    var body: some View {
        switch self {
        case .one:
            ViewOne()
        case .two:
            ViewTwo()
        case .three:
            ViewThree()
        }
    }
}

struct ViewOne: View {
    var body: some View {
        Text("ViewOne")
    }
}

struct ViewTwo: View {
    var body: some View {
        Text("ViewTwo")
    }
}

struct ViewThree: View {
    var body: some View {
        Text("ViewThree")
    }
}

struct Tab: Identifiable {
    let id = UUID()
    let title: String
    let desc: String
    let destination: SelectedView
}

struct DifferentView: View {
    var tabs: [Tab] = [
        Tab(title: "View One", desc: "Details of View One", destination: .one),
        Tab(title: "View Two", desc: "Details of View Two", destination: .two),
        Tab(title: "View Three", desc: "Details of View Three", destination: .three)
    ]
    var body: some View {
        NavigationView {
            List {
                ForEach(tabs) { tab in
                    NavigationLink(
                        destination: tab.destination,
                        label: {
                            VStack(alignment: .leading) {
                                Text(tab.title)
                                    .font(.headline).bold()
                                Text(tab.desc)
                                    .font(.callout)
                            }

                        })
                }
            }
            .navigationTitle("Title")
        }
    }
}

2      

Hi @roosterboy

Would it not be an idea to put the tabs Array in the enum as static then use ForEach(SelectedView.tabs) { tab in…

My thinking that you can have a file with all data, switch and cases in one place so if you need to add you could see it and leave the View to display it.

Thought?

Nigel

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.