您可以使用 @Binding
在视图之间共享数据。现在,通过调用 AddTodoView().backgroundColor
,您正在创建 AddTodoView
的新实例,而您实际上想要从层次结构中存在的同一视图中引用该值。
struct ContentView : View {
@State var backgroundColor : Color = Color(.systemBackground)
var body: some View {
ZStack {
backgroundColor //I'm using it in a ZStack,but you could also attach .background(backgroundColor) to any element in the hierarchy
AddTodoView(backgroundColor: $backgroundColor)
}
}
}
struct AddTodoView : View {
@Binding var backgroundColor: Color
var body: some View {
ColorPicker("Choose a color",selection: $backgroundColor)
}
}
本文链接:https://www.f2er.com/1188.html