Learn Courses My Dashboard

Infinit View in View, without NavigationLink

Hello,

I am really new to code and everything related, and I want to create a simple App to keep update of a collection that I have,

The thing is that basiclly, I want the user to go more and more detailed into what he is collecting… let me explain :

I want it to choose between two option, then inside this option there will be again two option, then multiple, etc etc until he find the exact things that he want to collect ( like zipo, so it would be by exemple in path-> OBJECT → CAN USE → LITTLE → ZIPO) and start checking it as “HAVE” everytime he saw a design that he already have, to say that he have this piece.

My problem is that using NavigationLink seems un-efficient, and may not work, I try searching a lot of option during the past week, and even if that help me finding other useful knowledge, I don’t see any optimal solution to my problem…

I basicaly need something link to a button which can take me to a view, then the a button inside the new view which can take me to another one etc etc…

I don’t know if that make sense, as it’s not “detail” and more full new view I don’t think NavigationView and Link could resolve what i search for…

If you could help a newbie :heart:
Thank you

Yeah, NavigationLink is pretty much what you would want to use. It’s not presenting a detail view but rather a stack of Views you drill down through until you find what you want. That’s pretty much what NavigationView (NavigationStack in iOS16+) and NavigationLink do best.

Perhaps the easiest way to do what you describe would be to have something like this very simplified example:

struct CategoryListView: View {
    let category: String
    let items: [String] = []
    
    init(_ category: String) {
        self.category = category
    }
    
    var body: some View {
        List {
            ForEach(items) { item in
                if itemHasSubItems(item) {
                    NavigationLink(item) {
                        CategoryListView(item)
                    }
                } else {
                    //your UI for allowing the user to check an item
                    //they already have
                }
            }
        }
        .onAppear {
            populateItems()
        }
    }
    
    func populateItems() {
        //use category to populate the items array however you want
        //this could be from Core Data or stored JSON or JSON retrieved
        //from an API or Firebase, etc
    }
    
    func itemHasSubItems(_ item: String) -> Bool {
        //given an item, determine if it has subitems the user
        //can drill down to, or if it is the end node
        //of your chain
    }
}

So, for instance, you start with OBJECT. You would display this with:

struct ItemsApp: App {
    var body: some Scene {
        WindowGroup {
            //if you are only targeting iOS 16 and above,
            //you could swap this out for NavigationStack
            NavigationView {
                CategoryListView("OBJECT")
            }
        }
    }
}

The screen would then list all the items under OBJECT and the user could tap the NavigationLink to drill down, say to CAN USE. Then the screen would display everything under CAN USE and the user can tap the NavigationLink to, say, LITTLE. And so on and so forth. It’s just nested NavigationLinks until you hit an end node, like ZIPO in your example.