gpt4 book ai didi

swift - 有没有办法将变量/ bool 值传递到 View 的 super View 中?

转载 作者:行者123 更新时间:2023-11-30 10:35:59 26 4
gpt4 key购买 nike

假设我将以下 sudocode 作为名为 Item 的 View

Item{
Rectangle()
.frame(width: 100, height: 50)
.onTapGesture{
isTapped.toggle()
}
}

它的 super View 中有多个项目,内容

Content{
VStack{
Item()
Item()
Item()
}
}

是否有一种方法可以让我将变量/ bool isTapped从Item向上转发到它的 super View ,并且它仍然是特定于Item的? (所以我知道哪个 Item 有什么 isTapped 值),例如,这可能吗?...

Content{
VStack{
Item().padding(.bottom, Item.isTapped ? 20 : 0)
Item().padding(.bottom, Item.isTapped ? 20 : 0)
Item().padding(.bottom, Item.isTapped ? 20 : 0)
}
}

编辑:需要注意的一个关键细节是项目的数量将由用户动态生成,因此我无法为每个项目创建一个变量

最佳答案

在父级中将变量声明为 @State 并将其作为 @Binding 传递给子级。

关于swift - 有没有办法将变量/ bool 值传递到 View 的 super View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58021505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com