如何根据 SwiftUI 中另一个视图的变量显示视图(在 ContentView 中)?
How do you display a view (in ContentView) depending on a variable of another view in SwiftUI?
基本上,在我的 ContentView 中,当绑定变量名称(不是 ContentView 中的变量名称,MapView class 中的变量名称)为真时,我试图创建一个 CreateItemButton()。所以...... ContentView 的伪代码看起来像:
struct ContentView: View {
@State private var variableName: Bool = false
var body: some View {
VStack {
ZStack {
MapView(variableName: $variableName)
//Display the CreateEventButton() view here if $variableName is true.
}
}
}
}
我希望我可以在 MapView() 视图上调用一些函数来选择性地显示 CreateEventButton() 视图,但我发现最接近我需要的是 .sheet( isPresented: $variableName) 函数....我需要视图直接显示在地图顶部,而不是拉出一个新的 sheet 和视图。让我知道是否可以添加任何内容以帮助使它更清楚!
在这里
ZStack {
MapView(variableName: $variableName)
if variableName {
CreateEventButton()
}
}
基本上,在我的 ContentView 中,当绑定变量名称(不是 ContentView 中的变量名称,MapView class 中的变量名称)为真时,我试图创建一个 CreateItemButton()。所以...... ContentView 的伪代码看起来像:
struct ContentView: View {
@State private var variableName: Bool = false
var body: some View {
VStack {
ZStack {
MapView(variableName: $variableName)
//Display the CreateEventButton() view here if $variableName is true.
}
}
}
}
我希望我可以在 MapView() 视图上调用一些函数来选择性地显示 CreateEventButton() 视图,但我发现最接近我需要的是 .sheet( isPresented: $variableName) 函数....我需要视图直接显示在地图顶部,而不是拉出一个新的 sheet 和视图。让我知道是否可以添加任何内容以帮助使它更清楚!
在这里
ZStack {
MapView(variableName: $variableName)
if variableName {
CreateEventButton()
}
}