Adding multi view to NavigationPath in one go

Hi

How can we add multi view to NavigationPath in one go like in code below maybe using something like path.append(mobiles.all) ? where all 3 views of mobiles Array get added at once ?

Kindest Regards

`struct NavigationPathView: View {

var mobiles: [Mobiles] = [.init (name: "iPhone", imageName: "folder", color: .mint),
                          .init (name: "Samsung", imageName: "pin", color: .pink),
                          .init (name: "Mac Pro", imageName: "book", color: .gray)]

@State var path = NavigationPath()

var body: some View {
    NavigationStack (path: $path) {
        List {
            Section ("Platforms") {
                Button("Go To Platform") {
                    path.append(mobiles.first!)
                }
                Button("Go To Mobile") {
                    path.append(mobiles.last!)
                }
                Button("Add All Mobiles") {
                    
                }
            }
        }
    }
    .navigationDestination(for: Mobiles.self) {mobile in
        ZStack {
            mobile.color.ignoresSafeArea()
            VStack {
                Image(systemName: mobile.imageName)
                Label (mobile.name, systemImage: mobile.imageName)
                    .font(.largeTitle).bold().foregroundColor(.white)
                Button("Go Home") {
                    path.removeLast(path.count)
                }
            }
            
        }
    }
}

}

Answered by DTS Engineer in 774474022
  • If your stack displays views that rely on only one kind of data, you can use a standard collection, like an array, to hold the data instead of a type-erased list of data representation.

You could use append(contentsOf:) method, which adds the elements of a sequence to the end of the array. For example:

struct ContentView: View {
    @State private var presentedFlavour: [Flavour] = []
    var body: some View {
        NavigationStack(path: $presentedFlavour) {
            List(presentedFlavour) { flavour in
                   NavigationLink(flavour.name, value: flavour)
               }
               .navigationDestination(for: Flavour.self) { flavour in
                   switch flavour {
                       ...
                   }
               }
        }
    }
    
    func showFlavour() {
        let flavourList: [Flavour] = [.chocolate, .strawberry, .vanilla]
        presentedFlavour.append(contentsOf: flavourList)
    }
}
Accepted Answer
  • If your stack displays views that rely on only one kind of data, you can use a standard collection, like an array, to hold the data instead of a type-erased list of data representation.

You could use append(contentsOf:) method, which adds the elements of a sequence to the end of the array. For example:

struct ContentView: View {
    @State private var presentedFlavour: [Flavour] = []
    var body: some View {
        NavigationStack(path: $presentedFlavour) {
            List(presentedFlavour) { flavour in
                   NavigationLink(flavour.name, value: flavour)
               }
               .navigationDestination(for: Flavour.self) { flavour in
                   switch flavour {
                       ...
                   }
               }
        }
    }
    
    func showFlavour() {
        let flavourList: [Flavour] = [.chocolate, .strawberry, .vanilla]
        presentedFlavour.append(contentsOf: flavourList)
    }
}

Thanks allot

Adding multi view to NavigationPath in one go
 
 
Q