[英]What does List var in NavigationLink mean in SwiftUI
The following is my code for my simple contentView以下是我的简单 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.
我已经通过有关 swift 语法的官方文档阅读了有关List和 go 的文档。 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. List 是一个结构体,它是一个视图,它在初始化自身时将闭包作为参数。
You can image this closure as a function, so that this function is kind of like this.你可以把这个闭包想象成一个 function,所以这个 function 有点像这样。
func imagineFunc(item: YourItem) -> YourRowContent { //some code goes here return YourRowContent } func imagineFunc(item: YourItem) -> YourRowContent { //这里有一些代码 return YourRowContent }
Note: above function is only for explanation.注:以上function仅作说明。 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.因此,当 ListView 想要创建其行之一时,List 会调用这个给定的闭包(imagineFunc)并获得一个 RowContent/行视图。
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).根据您的代码,当列表视图调用给定的闭包(对于每一行)时,您返回了一个 NavigationLink(您可以将其想象成一个按钮,当它在导航视图中时可以导航到另一个视图)。 So that each row becomes a NavigationLink in your List.
这样每一行都成为列表中的 NavigationLink。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.