简体   繁体   中英

What does List var in NavigationLink mean in SwiftUI

The following is my code for my simple contentView

struct ContentView: View {
    @State private var selection = 1;
    @State private var addFood = false;
    var listItems = [
        Food(name: "List Item One"),
        Food(name: "List Item Two")
    ]
    var body: some View {
        TabView(selection: $selection) {
            NavigationView {
                List(listItems){
                    food in NavigationLink(destination: FoodView(selec: selection)) {
                        Text(food.name)
                    }
                }.navigationBarTitle(Text("Fridge Items"), displayMode: .inline)
                .navigationBarItems(trailing:
                                        NavigationLink(destination: FoodView(selec: selection)) {
                                            Image(systemName: "plus.circle").resizable().frame(width: 22, height: 22)
                                        } )
            }
            .tabItem {
                Image(systemName: "house.fill")
                Text("Home")
            }
            .tag(1)
            
            
            Text("random tab")
                .font(.system(size: 30, weight: .bold, design: .rounded))
                .tabItem {
                    Image(systemName: "bookmark.circle.fill")
                    Text("profile")
                }
                .tag(0)
        }
        
    }
    
    
    
}

struct FoodView: View{
    var selec: Int?
    var body: some View{
        NavigationView{
            Text("food destination view \(selec!)");
        }
    }
}

I follow some tutorials to get to this point. However, I'm confused about the syntax

List(listItems){
                    food in NavigationLink(destination: FoodView(selec: selection)) {
                        Text(food.name)
                    }

What does the above line mean? My guess is that we list out the items we have, and then for each food we have, we add a navigation link for them. However, that's my guess and I want to know the true syntax behind this. I've already read the documentation about List and also go through the official documentation about swift syntax. I didn't find anything useful. I thought it was a closure first, but I found there is still a difference. Could anyone help, please.

List is a struct which is a View, and it takes a closure as a parameter when initialising itself.

You can image this closure as a function, so that this function is kind of like this.

func imagineFunc(item: YourItem) -> YourRowContent { //some code goes here return YourRowContent }

Note: above function is only for explanation. there are no such types.

so, when ListView wants to create one of its rows, then List call this given closure(imagineFunc) and get made a RowContent/row view.

According to your code when List view call the given closure(for each row) you have return a NavigationLink(You can imagine this as a button which can navigate to another view when it is inside navigation view). So that each row becomes a NavigationLink in your List.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM