WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Displaying a view from an array of views?

Forums > SwiftUI

Hi.

I've posted this question on stack: https://stackoverflow.com/questions/76397553/swiftui-dynamically-chose-view-from-an-array-of-views****

but people on here are more helpful.

Can someone please help?

2      

Hi!

As this answer correclty hints

Views don't belong in arrays, you can use an enum with an @ViewBuilder – lorem ipsum 23 mins ago

You can do as follows.

enum Screens {
    case viewA
    case viewB
}

struct ContentView: View {

    let arrayOfViews: [Screens] = [.viewA, .viewB] // etc.
    @State private var selectedIndex = 1

    var body: some View {
        ZStack {
            navigate(to: arrayOfViews[selectedIndex])
            //navigate(to: .viewA) // or this option
        }
    }

    @ViewBuilder
    func navigate(to screen: Screens) -> some View {
        switch screen {
        case .viewA:
            ViewA()
        case .viewB:
            ViewB()
        }
    }
}

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

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

2      

lorem ipsem is an arrogant dick that instantly downvotes EVERY question I ask on stack. He's been trolling me for months.

I solved it by wrapping them like this:

internal  let arrayOfViews    : [AnyView]   = [AnyView(ViewA()), AnyView(ViewB())]

3      

Well, of course if that fits your needs, it is absolutely fine to use AnyView. But remember this is type eraser and might not always be useful.

SwiftUI ships with a special view called AnyView, which can be used as a type erased wrapper to enable multiple view types to be returned from a single function or computed property, or to let us reference a view without having to know its underlying type. However, while there are cases in which we might need to use AnyView, it’s often best to avoid it as much as possible. That’s because SwiftUI uses a type-based algorithm to determine when a given view should be redrawn on screen, and since two AnyView-wrapped views will always look completely identical from the type system’s perspective (even if their underlying, wrapped types are different), performing this kind of type erasure significantly reduces SwiftUI’s ability to efficiently update our views.

full article is here https://www.swiftbysundell.com/articles/avoiding-anyview-in-swiftui/

2      

Yeah, people seem overly attached to not using AnyView when even Apple say there is a time and place for it; for me I found it. There are very few hard and fast rules in software engineering, besides Allman bracing being the only good bracing style.

2      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.