nachonavarro / Pages

📖 A lightweight, paging view solution for SwiftUI
MIT License
552 stars 52 forks source link

Navigation Link #5

Closed VowerOrg closed 4 years ago

VowerOrg commented 4 years ago

How do we integrate navigation link with pages? I have tried the code below. The view shows it was clicked (visually), however no desination is actually navigated to.

    var body: some View {
        ModelPages(cars) { index, car in
NavigationLink(destination: DestinationView(car: car)) {
            Text("The \(index) car is a \(car.model)")
                .padding(50)
                .foregroundColor(.white)
                .background(Color.blue)
                .cornerRadius(10)
        }
    }
}
homanp commented 4 years ago

@VowerOrg did you find a solution to it?

dri94 commented 4 years ago

+1

nachonavarro commented 4 years ago

Are you sure you wrapped everything in a NavigationView? This issue is unrelated to Pages, but a NavigationLink only works inside of a NavigationView. Update Pages and try this:

import SwiftUI
import Pages

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

struct Car {
    var model: String
}

struct CarsView: View {
    let cars = [Car(model: "Ford"), Car(model: "Ferrari")]
    @State var index: Int = 0

    var body: some View {
        NavigationView {
            ModelPages(cars, currentPage: $index) { pageIndex, car in
                NavigationLink(destination: SettingsView(), label: {
                    Text("Go to my settings...")
                })
            }
        }
    }
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        CarsView()
    }
}

I'm closing this for now, reopen it if this doesn't work for you.