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

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Reply to this topic…

You need to create an account or log in to reply.

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.